pthread_join()阻塞等待函数
pthread_t tid;
pthread_create(&tid, NULL, thread_run,NULL);
pthread_join(tid,NULL);
#include "stdafx.h"
#include
#include
#include
#pragma comment(lib, "pthreadVC2.lib")static int count = 0;void* thread_run(void* parm)
{for (int i&#61;0;i<5;i&#43;&#43;){count&#43;&#43;;printf("The thread_run method count is &#61; %d\n",count);Sleep(1000);}return NULL;
}int main()
{pthread_t tid;pthread_create(&tid, NULL, thread_run,NULL);// 加入pthread_join后&#xff0c;主线程"main"会一直等待直到tid这个线程执行完毕自己才结束// 一般项目中需要子线程计算后的值就需要加join方法pthread_join(tid,NULL);// 如果没有join方法可以看看打印的顺序printf("The count is &#61; %d\n",count);getchar();return 0;
}
不阻塞