日期:2014-05-16 浏览次数:20979 次
for(int i=0;i<5;i++) { pthread_create(); //这行代码该怎么写才能在循环结束之后创建了5个线程? }
#include <unistd.h> #include <pthread.h> #include <stdio.h> void *ping_pthead(void* arg) { while(1) { int id = arg; printf("arg = %d, ping\n", id); sleep(2); } } int main(int argc, char** argv) { pthread_t pid[5]; int i = 0; for (i = 0; i < 5; i++) { pthread_create(&pid[i], NULL, ping_pthead, (void*)i); sleep(1); printf("i=%d\n", i); } printf("............\n"); for (i = 0; i < 5; i++) { pthread_join(pid[i], NULL); } return 0; }