c語言多線程編程使用示例
#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 %d\n", i);
err = pthread_create(&child[i], NULL, test, (void *) i);
if(err) {
printf("Can't create thread %d\n", i);
exit(0);
}
}
for(i = 0; i < THREAD_NUM; i++)
pthread_join(child[i], NULL);
printf("Thread initialize\n");
return 0;
}
相關文章
C語言順序表的基本結(jié)構(gòu)與實現(xiàn)思路詳解
順序表是用一段物理地址連續(xù)的存儲單元依次存儲數(shù)據(jù)元素的線性結(jié)構(gòu),一般情況下采用數(shù)組存儲。本文將通過示例為大家講解一下順序表的基本操作,需要的可以參考一下2023-02-02C++ Thread實現(xiàn)簡單的socket多線程通信
本文主要介紹了C++ Thread實現(xiàn)簡單的socket多線程通信,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07