c语言多线程编程使用示例
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:#include#include#include#defineTHREAD_NUM10void*test(void*arg...
复制代码 代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define THREAD_NUM 10
void *test(void *args) {
printf("tid %d: i say 'Hello'.n", args);
return NULL;
}
int main() {
int i, err;
pthread_t child[THREAD_NUM];
for(i = 0; i < THREAD_NUM; i++) {
printf("Creating thread %dn", i);
err = pthread_create(&child[i], NULL, test, (void *) i);
if(err) {
printf("Can't create thread %dn", i);
exit(0);
}
}
for(i = 0; i < THREAD_NUM; i++)
pthread_join(child[i], NULL);
printf("Thread initializen");
return 0;
}