Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5707855
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: C/C++

2006-10-05 09:55:50

#include
#include
#include

void *thread(void *vargp)
{
    printf("Helo,world!\n");
    exit(0);
}

int main()
{
    pthread_t tid;
    pthread_create(&tid,NULL,thread,NULL);
    pthread_join(tid,NULL);
    
    printf("Thread is over!\n");

    exit(0);
}
这样的话,在线程例程中调用exit,将会使整个进程中止,不仅仅是这个例程。
将只会打印Helo,world!;不会打印hread is over!。
中止线程,可以通过调用pthread_exit来实现。

#include
#include
#include

void *thread(void *vargp)
{
    void *pthread_return;
    pthread_t id;

    id = pthread_self();
    printf("The thread's ID is: %d.\n",id);

    pthread_exit(pthread_return);
    printf("Helo,world!\n");
}

int main()
{
    pthread_t tid;
    pthread_create(&tid,NULL,thread,NULL);
    pthread_join(tid,NULL);
   
    printf("Thread is over!\n");

    exit(0);
}
上面的程序将不会打印Hello,world!,但是还会打印主线程里面的Thread is over!
 
阅读(831) | 评论(0) | 转发(0) |
0

上一篇:多线程编程第一例

下一篇:Tomcat安装

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