码迷,mamicode.com
首页 > 其他好文 > 详细

thread_1

时间:2014-09-27 01:44:28      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   sp   div   art   c   

#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

标签:style   blog   color   io   ar   sp   div   art   c   

原文地址:http://www.cnblogs.com/leijiangtao/p/3995759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!