文档介绍:函数说明pthread_create:创建一个线程linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。#include<>intpthread_create(pthread_t*restricttidp,constpthread_attr_t*restrictattr,void*(*start_rtn)(void),void*restrictarg);Returns:0ifOK,errornumberonfailure第一个参数为指向线程标识符的指针。第二个参数用来设置线程属性。第三个参数是线程运行函数的起始地址。最后一个参数是运行函数的参数。另外,因为pthread并非Linux系统的默认库,在编译时注意加上-lpthread参数,以调用静态链接库。pthread_join:用来等待一个线程的结束。函数原型为:externintpthread_join__P((pthread_t__th,void**__thread_return));第一个参数为被等待的线程标识符,第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们下面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现。另外需要说明的是,一个线程不能被多个线程等待,也就是说对一个线程只能调用一次pthread_join,否则只有一个能正确返回,其他的将返回ESRCH错误。范例:////子线程阻塞,等待信号,然后输出字符串//主线程从键盘录入字符,给子线程发信号。#include<>#include<>#include<>#include<>#include<>pthread_ttid;sigset_tset;voidmyfunc(){printf("hello\n");}staticvoid*mythread(void*p){intsignum;while(1){sigwait(&set,&signum);if(SIGUSR1==signum)myfunc();if(SIGUSR2==signum){printf("Iwillsleep2secondandexit\n");sleep(2);break;}}}intmain(){chartmp;void*status;sigemptyset(&set);sigaddset(&set,SIGUSR1);sigaddset(&set,SIGUSR2);sigprocmask(SIG_SETMASK,&set,NULL);pthread_create(&tid,NULL,mythread,NULL);while(1){printf(":");scanf("%c",&tmp);if('a'==tmp){pthread_kill(tid,SIGUSR1);//发送SIGUSR1,打印字符串。}elseif('q'==tmp){pthread_kill(tid,SIGUSR2);//发