国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Linux編程之多線程常用函數(shù)使用實(shí)例

2019-11-17 04:57:38
字體:
供稿:網(wǎng)友

  Paul Chen     2006-03-16         BeiJing

//以下代碼均來自《linux C 編程》一書,本人對(duì)其中代碼做了一些改進(jìn),使之更有利于系統(tǒng)學(xué)習(xí)GNU C標(biāo)準(zhǔn)庫中Pthread。

//非凡注重:makefile中的編譯選項(xiàng)中一定要包含-lpthread(即連接pthread庫)

makefile:

demo:demo1.o
    gcc -o demo demo1.o -lpthread
   
demo1.o:demo1.c   
    gcc -c demo1.c
   
clean:
    rm demo demo1.c   


demo1.c:

#include <stdio.h>
#include <pthread.h>

//used in test1
pthread_once_t once=PTHREAD_ONCE_INIT;

//used in test2
pthread_mutex_t mutex;
pthread_cond_t cond;

//used in test3
pthread_key_t key; 

void once_run(void)
{
  PRintf("once_run in thread %d/n",pthread_self());
}

void * child1(void *arg)
{
  int tid=pthread_self();
  printf("thread child1 %d enter/n",tid);
  pthread_once(&once,once_run);
  //sleep(3);
  printf("thread child1 %d returns/n",tid);
}

void * child2(void *arg)
{
  int tid=pthread_self();
  printf("thread child2 %d enter/n",tid);
  pthread_once(&once,once_run);
  //sleep(3);
  printf("thread child2 %d returns/n",tid);
}

void * child3(void *arg)
{
   pthread_cleanup_push(pthread_mutex_unlock,&mutex); /* comment 1 */
   while(1){
      printf("thread child3 get running /n");
      printf("thread child3 pthread_mutex_lock returns %d/n",
      pthread_mutex_lock(&mutex));
      pthread_cond_wait(&cond,&mutex);
      printf("thread child3 condition applied/n");
      pthread_mutex_unlock(&mutex);
      sleep(5);
   } 
   pthread_cleanup_pop(0); /* comment 2 */
}

void *child4(void *arg)
{
   while(1){
      sleep(3); /* comment 3 */
      printf("thread child4 get running./n");
    
  printf("thread child4 pthread_mutex_lock returns %d/n",
      pthread_mutex_lock(&mutex));
      pthread_cond_wait(&cond,&mutex);
      printf("thread child4 condition applied/n");
      pthread_mutex_unlock(&mutex);
      sleep(1);
   }
}

void echomsg(int t)
{
   printf("destrUCtor excuted in thread %d,param=%d/n",pthread_self(),t);
}

void * child5(void *arg)
{
    int tid=pthread_self();
    printf("thread child5 %d enter/n",tid);
    pthread_setspecific(key,(void *)tid);
    sleep(2);
    printf("thread child5 %d returns %d/n",tid,pthread_getspecific(key));
    sleep(5);
}

void * child6(void *arg)
{
    int tid=pthread_self();
    printf("thread child6 %d enter/n",tid);
    pthread_setspecific(key,(void *)tid);
    sleep(1);
    printf("thread child6 %d returns %d/n",tid,pthread_getspecific(key));
    sleep(5);
}

int main(void)
{
  int tid1,tid2,tid3,tid4,tid5,tid6;
  printf("hello/n");
 
  printf("/n/n/n");
 
  //test1:pthread_once,pthread_self
  pthread_create(&tid1,NULL,child1,NULL);
  pthread_create(&tid2,NULL,child2,NULL);
  pthread_join(tid1,NULL);
  pthread_join(tid2,NULL);
 
  printf("/n/n/n");

  //test3:pthread_key_create,pthread_setspecific,pthread_getspecific
  pthread_key_create(&key,echomsg);
  pthread_create(&tid5,NULL,child5,NULL);
  pthread_create(&tid6,NULL,child6,NULL);
  pthread_join(tid5,NULL);
  pthread_join(tid6,NULL); 
  pthread_key_delete(key);  
 
  printf("/n/n/n");
   
  //test2:pthread_mutex_lock,pthread_mutex_unlock,pthread_cond_wait,pthread_cleanup_pop /
          pthread_cond_signal,pthread_exit
  pthread_create(&tid3,NULL,child3,NULL);
  pthread_create(&tid4,NULL,child4,NULL);
  pthread_join(tid3,NULL);
  pthread_join(tid4,NULL);
  do{
     sleep(2); /* comment 4 */
     pthread_cancel(tid3); /* comment 5 */
 
    sleep(2); /* comment 6 */
     pthread_cond_signal(&cond);
  }while(1);
  sleep(100);
  pthread_exit(0);
 
  printf("/n/n/n");
 
  printf("main thread exit/n");
  return 0;
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 锡林郭勒盟| 榆中县| 蚌埠市| 安仁县| 泗阳县| 新晃| 五原县| 家居| 三都| 孟州市| 来安县| 涞源县| 滦平县| 高青县| 鄢陵县| 旬邑县| 通城县| 进贤县| 白朗县| 建昌县| 怀集县| 威远县| 冀州市| 明水县| 靖江市| 晋江市| 永定县| 兴隆县| 宜兰县| 玛多县| 周口市| 寿宁县| 石屏县| 漳州市| 德州市| 沙洋县| 清远市| 崇义县| 伊川县| 茂名市| 象州县|