成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

理解線程3 c語言示例線程基本操作

A Loity / 714人閱讀

摘要:基本線程的動作繼續(xù)之前語言線程的文章文章文章來了解基本的線程操作。屬性用完后對其進行清理回收通過共享的變量來檢測子線程是否已經(jīng)結(jié)束代碼如下設(shè)置調(diào)度屬性線程庫提供以下調(diào)度策略先進先出調(diào)度。

基本線程的動作

繼續(xù)之前C語言線程的文章:文章1 文章2 來了解基本的線程操作。

設(shè)置線程屬性 設(shè)置脫離狀態(tài)

下面代碼中關(guān)鍵的地方在于:

通過 res = pthread_attr_init(&thread_attr); 初始化一個線程屬性

通過 res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); 將屬性設(shè)置為脫離狀態(tài)(PTHREAD_CREATE_DETACHED),即不能通過調(diào)用 pthread_join 來獲得另一個線程的退出狀態(tài)

另外還有一個常用的默認狀態(tài)是 PTHREAD_CREATE_JOINABLE ,可以允許兩個線程重新合并。

屬性用完后對其進行清理回收 (void)pthread_attr_destroy(&thread_attr);

通過共享的變量 thread_finished 來檢測子線程是否已經(jīng)結(jié)束

代碼如下:

#include 
#include 
#include 
#include 

void *thread_function(void *arg);

char message[] = "Hello World";
int thread_finished = 0;

int main() {
  int res;
  pthread_t a_thread;

  pthread_attr_t thread_attr;

  res = pthread_attr_init(&thread_attr);
  if (res != 0) {
    perror("Attribute creation failed");
    exit(EXIT_FAILURE);
  }
  res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
  if (res != 0) {
    perror("Setting detached attribute failed");
    exit(EXIT_FAILURE);
  }
  res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);
  if (res != 0) {
    perror("Thread creation failed");
    exit(EXIT_FAILURE);
  }
  (void)pthread_attr_destroy(&thread_attr);
  while(!thread_finished) {
    printf("Waiting for thread to say it"s finished...
");
    sleep(1);
  }
  printf("Other thread finished, bye!
");
  exit(EXIT_SUCCESS);
}

void *thread_function(void *arg){
  printf("thread_function is running. Argument was %s
", (char *)arg);
  sleep(4);
  printf("Second thread setting finished flag, and exiting now
");
  thread_finished = 1;
  pthread_exit(NULL);
}
設(shè)置調(diào)度屬性

線程庫提供以下調(diào)度策略:

| SCHED_FIFO  | 先進先出 (FIFO) 調(diào)度。每個線程都有一個固定的優(yōu)先級;當多個線程具有相同的優(yōu)先級時,它們按照先進先出 (FIFO) 的順序運行直到完成 |
| SCHED_RR    | 循環(huán) (RR) 調(diào)度。每個線程都有固定的優(yōu)先級;當多個線程具有相同的優(yōu)先級時,它們按照先進先出 (FIFO) 的順序在一個 固定的時間片內(nèi)運行。 |
| SCHED_OTHER | 缺省的 AIX? 調(diào)度。每個線程都有一個由調(diào)度程序根據(jù)線程的活動動態(tài)修改的初始優(yōu)先級;線程的執(zhí)行是按時間分割的。在其他系統(tǒng)上,這個調(diào)度策略可能會不同。 |

設(shè)置調(diào)度屬性和設(shè)置很相似:

#include 
#include 
#include 
#include 

void *thread_function(void *arg);

char message[] = "Hello World";
int thread_finished = 0;

int main() {
    int res;
    pthread_t a_thread;
    pthread_attr_t thread_attr;

    int max_priority;
    int min_priority;
    struct sched_param scheduling_value;

    res = pthread_attr_init(&thread_attr);
    if (res != 0) {
        perror("Attribute creation failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_attr_setschedpolicy(&thread_attr, SCHED_OTHER);
    if (res != 0) {
        perror("Setting schedpolicy failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
    if (res != 0) {
        perror("Setting detached attribute failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_create(&a_thread, &thread_attr, thread_function, (void *)message);
    if (res != 0) {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
    max_priority = sched_get_priority_max(SCHED_OTHER);
    min_priority = sched_get_priority_min(SCHED_OTHER);
    scheduling_value.sched_priority = min_priority;
    res = pthread_attr_setschedparam(&thread_attr, &scheduling_value);
    if (res != 0) {
        perror("Setting schedpolicy failed");
        exit(EXIT_FAILURE);
    }
    (void)pthread_attr_destroy(&thread_attr);
    while(!thread_finished) {
        printf("Waiting for thread to say it"s finished...
");
        sleep(1);
    }
    printf("Other thread finished, bye!
");
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
    printf("thread_function is running. Argument was %s
", (char *)arg);
    sleep(4);
    printf("Second thread setting finished flag, and exiting now
");
    thread_finished = 1;
    pthread_exit(NULL);
}
取消線程

通過 int pthread_cancel(pthread_t thread); 來請求一個線程終止

通過 int pthread_setcancelstate(int state, int *oldstate) 來設(shè)置接受的進程是允許取消請求還是忽略它

通過 int pthread_setcanceltype(int type, int *oldtype) 來設(shè)置取消類型, PTHREAD_CANCEL_ASYCHRONOUS 代表接收到取消請求后立即行動, THREAD_CANCEL_DEFERRED 表示在接收到請求后,等待函數(shù)執(zhí)行下述動作之一后才取消線程: pthread_join, pthread_cond_wait, pthread_cond_timeout, pthread_test_cancel, sem_wait, sigwait

代碼如下:

#include 
#include 
#include 
#include 

void *thread_function(void *arg);

int main () {
  int res;
  pthread_t a_thread;
  void *thread_result;

  res = pthread_create(&a_thread, NULL, thread_function, NULL);
  if (res != 0){
    perror("Thread creation failed");
    exit(EXIT_FAILURE);
  }
  sleep(3);
  printf("Caceling thread...
");
  res = pthread_cancel(a_thread);
  if (res != 0){
    perror("Thread cancelation failed");
    exit(EXIT_FAILURE);
  }
  printf("Waiting for thread to finish...
");
  res = pthread_join(a_thread, &thread_result);
  if (res != 0) {
    perror("Thread join failed");
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
  int i, res;
  res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  if (res != 0) {
    perror("Thread pthread_setcalcelstate failed");
    exit(EXIT_FAILURE);
  }
  res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
  if (res != 0) {
    perror("Thread pthread_setcanceltype failed");
    exit(EXIT_FAILURE);
  }
  printf("thread_function is running
");
  for(i=0; i<10; i++) {
    printf("Thread is still running (%d)...
", i);
    sleep(1);
  }
  pthread_exit(0);
}
主線程創(chuàng)建多個線程示例

代碼如下:

#include 
#include 
#include 
#include 

#define NUM_THREADS 6

void *thread_function(void *arg);

int main() {
    int res;
    pthread_t a_thread[NUM_THREADS];
    void *thread_result;
    int lots_of_threads;

    for(lots_of_threads = 0; lots_of_threads < NUM_THREADS; lots_of_threads++) {

        res = pthread_create(&(a_thread[lots_of_threads]), NULL, thread_function, (void *)&lots_of_threads);
        if (res != 0) {
            perror("Thread creation failed");
            exit(EXIT_FAILURE);
        }
        sleep(1);
    }
    printf("Waiting for threads to finish...
");
    for(lots_of_threads = NUM_THREADS - 1; lots_of_threads >= 0; lots_of_threads--) {
        res = pthread_join(a_thread[lots_of_threads], &thread_result);
        if (res == 0) {
            printf("Picked up a thread
");
        }
        else {
            perror("pthread_join failed");
        }
    }
    printf("All done
");
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg) {
    int my_number = *(int *)arg;
    int rand_num;

    printf("thread_function is running. Argument was %d
", my_number);
    rand_num=1+(int)(9.0*rand()/(RAND_MAX+1.0));
    sleep(rand_num);
    printf("Bye from %d
", my_number);
    pthread_exit(NULL);
}

運行結(jié)果如下:

thread_function is running. Argument was 0
Bye from 0
thread_function is running. Argument was 1
thread_function is running. Argument was 2
Bye from 1
thread_function is running. Argument was 3
thread_function is running. Argument was 4
thread_function is running. Argument was 5
Waiting for threads to finish...
Bye from 5
Picked up a thread
Bye from 3
Bye from 2
Bye from 4
Picked up a thread
Picked up a thread
Picked up a thread
Picked up a thread
Picked up a thread
All done
了解更多

Posix多線程編程—線程屬性

參考資料

《Linux 程序設(shè)計》

http://www.ibm.com/support/kn...

PS

不得不承認,我失敗了。曾計劃每天分享一篇python相關(guān)知識點但沒有做到。失敗的原因想找可以找很多比如最近在做一個小的項目、這幾天出去聚會沒有時間、工作出現(xiàn)問題加班到比較晚等等,然而總結(jié)起來不外乎在心里它的重要程度是怎么樣的。我反思了下幾乎做到一天一篇的這一個月過程做出改變,不再要求一天一篇,而是當我有好的素材要分享時才分享,這樣與我與大家都是一件好事,我可以動態(tài)調(diào)度自己的精力和注意力,比如最近實現(xiàn)了一半的一個odoo項目依賴關(guān)系分析器可以盡快把它做完,對于讀者來說也可以減少干擾看到更好的分享而不是像我之前有幾篇那樣劃水的。但每周應(yīng)該會有3-4篇Python的知識可以分享。另外我目前的工作是基于Odoo的,我計劃嘗試做一些基礎(chǔ)的教程來分享這個我熟悉的框架,如果有進展一定會告知感興趣的讀者。感謝讀者。

最后向漩渦鳴人致敬,朝他的“說到做到,這就是我的忍道”努力。

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/38241.html

相關(guān)文章

  • 理解線程1 C語言示例的程序

    摘要:一個簡單的語言實現(xiàn)的線程示例在看時,為了更好的理解線程的概念,書中列舉了這樣一個小例子將程序編譯鏈接后運行,可以看到下面這樣的結(jié)果這里使用創(chuàng)建新線程,的定義如下根據(jù)要求,只有一個指向的指針作為參數(shù),返回的也是指向的指針。 一個簡單的C語言實現(xiàn)的線程示例 在看《Beginning Linux Programming》時,為了更好的理解線程的概念,書中列舉了這樣一個小例子: #inclu...

    lncwwn 評論0 收藏0
  • [Java并發(fā)-2]Java如何解決可見性問題的

    摘要:誕生之處就支持多線程,所以自然有解決這些問題的辦法,而且在編程語言領(lǐng)域處于領(lǐng)先地位。,線程規(guī)則這條是關(guān)于線程啟動的。在語言里面,的語義本質(zhì)上是一種可見性,意味著事件對事件來說是可見的,無論事件和事件是否發(fā)生在同一個線程里。 之前我們說了:1,可見性2,原子性3,有序性3個并發(fā)BUG的之源,這三個也是編程領(lǐng)域的共性問題。Java誕生之處就支持多線程,所以自然有解決這些問題的辦法,而且在編...

    lk20150415 評論0 收藏0
  • JS高級入門教程

    摘要:解析首先簡稱是由歐洲計算機制造商協(xié)會制定的標準化腳本程序設(shè)計語言。級在年月份成為的提議,由核心與兩個模塊組成。通過引入統(tǒng)一方式載入和保存文檔和文檔驗證方法對進行進一步擴展。其中表示的標記位正好是低三位都是。但提案被拒絕了。 JS高級入門教程 目錄 本文章定位及介紹 JavaScript與ECMAScript的關(guān)系 DOM的本質(zhì)及DOM級介紹 JS代碼特性 基本類型與引用類型 JS的垃...

    zsy888 評論0 收藏0
  • 雙重檢查鎖定與延遲初始化

    摘要:基于的雙重檢查鎖定的解決方案對于前面的基于雙重檢查鎖定來實現(xiàn)延遲初始化的方案指示例代碼,我們只需要做一點小的修改把聲明為型,就可以實現(xiàn)線程安全的延遲初始化。 雙重檢查鎖定的由來 在java程序中,有時候可能需要推遲一些高開銷的對象初始化操作,并且只有在使用這些對象時才進行初始化。此時程序員可能會采用延遲初始化。但要正確實現(xiàn)線程安全的延遲初始化需要一些技巧,否則很容易出現(xiàn)問題。比如,下...

    yvonne 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<