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

王朝system·作者佚名  2006-03-18
窄屏简体版  字體: |||超大  

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;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航