您的位置:首页 > 其它

pthread_create()函数样例

2017-05-18 14:42 369 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  函数