您的位置:首页 > 其它

多线程相关知识

2017-01-05 16:57 281 查看
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NUM_THREADS 4

typedef struct{
int threadId;
}threadParm_t;

void  *threadFunc(void *parm)
{
threadParm_t *p=(threadParm_t *)parm;
fprintf(stdout,"Hello world from thread%d\n",p->threadId);
pthread_exit((void *)&(p->threadId));

return  0;
}

int  main(int argc,char *argv[])
{
int i;
int *res;
res=(int *)malloc(sizeof(int));
pthread_t thread[NUM_THREADS];
threadParm_t threadParm[NUM_THREADS];

for(i=0;i<NUM_THREADS;i++)
{
threadParm[i].threadId=i;
pthread_create(&thread[i],NULL,threadFunc,(void *)&threadParm[i]);
}

for(i=0;i<NUM_THREADS;i++)
{
pthread_join(thread[i],(void **)&res);

fprintf(stdout,"Thread %d has exited.\n",*res);
}

system("pause");
return 0;
}

参考网址:http://blog.chinaunix.net/uid-26275986-id-3886498.html

参考网址:http://blog.csdn.net/morewindows/article/details/17488865

参考网址:http://blog.csdn.net/yasi_xi/article/details/19112077              pthread_mutex_lock

参考网址:http://www.linuxcommand.org/man_pages/gcc1.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: