pthread_create

 

 

 

 

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

void* test(void* args) {
    int a =  *(int *) args;
    printf("a:%d\n", a);
}

main() {
    pthread_t pid;
    int x = 123;
    pthread_create(&pid, NULL, &test, &x);
    //sleep(1);

    printf("-------------\n");
}

上面这段程序运行结果为

-------------
a:123
a:123

很奇葩?实际上是因为变量x内存被释放,导致test线程中出现未知的异常

加上sleep(1)后能保证线程创建时传递的值使用时是ok的

 

a:123
-------------

 

posted @ 2018-01-25 10:27  牧 天  阅读(165)  评论(0编辑  收藏  举报