首页 > 代码库 > thread_1

thread_1

#include <stdlib.h> #include <pthread.h>  #include <stdio.h> #include <string.h> void * start_routine(void *arg) {         char * a;        printf("thread runing,%s\n", (char *)arg);        a = malloc(sizeof(char)*6);        memset(a, a, 5);       a[5] = \0;       pthread_exit((void *)a);       //return (void *)a; }        int main(int argc, char *argv[]) {         pthread_t  tid;        char c[4], *d;        memset(c, c, 3);        c[3] = \0;        pthread_create(&tid, NULL, start_routine, (void *)c);         printf("pthread id :%u\n", tid);        pthread_join(tid, (void **)&d);         printf("main: %s\n", d);        free(d);         return 0; }  /*pthread_join一般是主线程来调用,用来等待子线程退出,因为是等待,所以是阻塞的,一般主线程会依次join所有它创建的子线程。pthread_exit一般是子线程调用,用来结束当前线程。子线程可以通过pthread_exit传递一个返回值,而主线程通过pthread_join获得该返回值,从而判断该子线程的退出是正常还是异常。*///查阅线程的退出方式   

 

thread_1