pthread_create()函数样例

来源:互联网 发布:采样率转换算法 编辑:程序博客网 时间:2024/06/06 01:46
#include <stdio.h>#include <pthread.h>#include <unistd.h>void* client_pthread(void* arg){    printf("call client_pthread()\n");    while(1)    {           sleep(3);        printf("call client_pthread()\n");    }}void* client_pthread_two(void* date_two){    printf("call client_pthread_two()\n");    while(1)    {           sleep(3);        printf("call client_pthread_two()\n");    }}void test_pthread_client(void){    int ret = 0;    pthread_t pthread_id;    pthread_t pthread_id_two;    printf("call test_pthread_client()\n");    ret = pthread_create(&pthread_id, NULL, client_pthread, NULL);    if(0 != ret)    {        printf("create pthread false\n");        return;    }    ret = pthread_create(&pthread_id_two, NULL, client_pthread_two, NULL);    if(0 != ret)    {        printf("create pthread false\n");        return;    }    pthread_join(pthread_id,NULL);  //加入pthread_join后,主线程会一直等待直到等待的线程结束自己才结束,使创建的线程有机会执行。    pthread_join(pthread_id_two,NULL);  }int main(){    printf("call main()\n");    test_pthread_client();    return 0;}