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

全部博文(108)

文章存档

2015年(20)

2014年(88)

我的朋友

分类: C/C++

2014-04-04 12:56:04

原文地址:线程属性(线程分离) 作者:luozhiyong131

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

void mythread()
{
    printf("thread is pthread:n");
    sleep(1);
    printf("thread is exitingn");
    int thread_finished = 1;
    pthread_exit(NULL);
}

int main()
{
    pthread_t id;
    pthread_attr_t thread_attr;
    int ret,thread_finished;
    ret = pthread_attr_init(&thread_attr);
    if(ret!=0)
    {
        printf("pthread_attr_init errorn");
        exit(1);
    }
    
    ret = pthread_attr_setdetachstate(&thread_attr,PTHREAD_CREATE_DETACHED);
    if(ret!=0)
    {
        printf("pthread_attr_setdetachstate errorn");
        exit(1);
    }
    
    ret = pthread_create(&id,&thread_attr,(void *)mythread,NULL);
    if(ret!=0)
    {
        printf("create pthread errorn");
        exit(1);
    }    
    
    while(!thread_finished)
    {
        printf("waitint for thread finished..n");
        sleep(1);
    }
    printf("Donen");
    return 0;    
}



阅读(901) | 评论(0) | 转发(0) |
0

上一篇:select,poll,epoll用法

下一篇:C++STL学习经典

给主人留下些什么吧!~~