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;
}

posted on 2019-07-04 15:12  荷楠仁  阅读(232)  评论(0编辑  收藏  举报

导航