ARTS-S C语言多线程传参数
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *thread_func(void *arg){
int a = *(int *)arg;
printf("tid=%lu,a=%d\n", pthread_self(), a);
return ((void*)0);
}
int main() {
for (int i = 0; i < 10; i++) {
int err;
pthread_t tid;
int* a = (int*)malloc(sizeof(int));
*a = i;
err = pthread_create(&tid, NULL, thread_func, a);
if (err != 0) {
fprintf(stderr,"create thread fail.\n");
exit(-1);
}
}
sleep(5);
return 0;
}