首页 > 学院 > 开发设计 > 正文

Linux编程之多线程常用函数使用实例

2019-11-17 04:57:38
字体:
来源:转载
供稿:网友

  Paul Chen     2006-03-16         BeiJing

//以下代码均来自《linux C 编程》一书,本人对其中代码做了一些改进,使之更有利于系统学习GNU C标准库中Pthread。

//非凡注重:makefile中的编译选项中一定要包含-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;
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表