成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專(zhuān)欄INFORMATION COLUMN

理解線程3 c語(yǔ)言示例線程基本操作

A Loity / 680人閱讀

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

基本線程的動(dòng)作

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

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

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

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

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

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

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

通過(guò)共享的變量 thread_finished 來(lái)檢測(cè)子線程是否已經(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)度屬性

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

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

設(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);
}
取消線程

通過(guò) int pthread_cancel(pthread_t thread); 來(lái)請(qǐng)求一個(gè)線程終止

通過(guò) int pthread_setcancelstate(int state, int *oldstate) 來(lái)設(shè)置接受的進(jìn)程是允許取消請(qǐng)求還是忽略它

通過(guò) int pthread_setcanceltype(int type, int *oldtype) 來(lái)設(shè)置取消類(lèi)型, PTHREAD_CANCEL_ASYCHRONOUS 代表接收到取消請(qǐng)求后立即行動(dòng), THREAD_CANCEL_DEFERRED 表示在接收到請(qǐng)求后,等待函數(shù)執(zhí)行下述動(dòng)作之一后才取消線程: 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)建多個(gè)線程示例

代碼如下:

#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);
}

運(yùn)行結(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è)計(jì)》

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

PS

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

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

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/38241.html

相關(guān)文章

  • 理解線程1 C語(yǔ)言示例的程序

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

    lncwwn 評(píng)論0 收藏0
  • [Java并發(fā)-2]Java如何解決可見(jiàn)性問(wèn)題的

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

    lk20150415 評(píng)論0 收藏0
  • JS高級(jí)入門(mén)教程

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

    zsy888 評(píng)論0 收藏0
  • 雙重檢查鎖定與延遲初始化

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

    yvonne 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<