Chinaunix首页 | 论坛 | 博客
  • 博客访问: 247113
  • 博文数量: 108
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 314
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-29 10:58
文章分类

全部博文(108)

文章存档

2015年(20)

2014年(88)

我的朋友

分类: 嵌入式

2014-04-03 09:37:20

int pthread_join(pthread_t th, void **thread_return);

    th: 指定了将要等待的线程标识符
    thread_return: 它指向另外一个指针,而后者指向线程的返回值
    成功时返回“0”,失败时返回一个错误代码
    pthread_join相当于进程用来等待子进程的wait函数

作用:在线程结束后把它们归并到一起




#
include <pthread.h>
#include <unistd.h>
#include <stdio.h>
void *thread(void *str)
{
    int i;
    for (= 0; i < 3; ++i)
    {
        sleep(2);
        printf( "This in the thread : %dn" , i );
    }
    return NULL;
}

int main()
{
    pthread_t pth;
    int i;

    /*创建线程并执行线程执行函数*/
    int ret = pthread_create(&pth, NULL, thread, NULL);
    
    printf("The main process will be to run,but will be blocked soonn");
    
    /*阻塞等待线程退出*/
    pthread_join(pth, NULL);

    printf("thread was exitn");
    for (= 0; i < 3; ++i)
    {
        sleep(1);
        printf( "This in the main : %dn" , i );
    }
    
    return 0;
}
阅读(858) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~