线程学习之三_线程结束

线程可以通过下面的方法来结束执行:

1、从线程的第一个函数(本线程开始执行的函数)返回;

2、调用pthread_exit()函数;

3、从main()函数中调用返回或者调用exit();

4、使用取消函数执行终止操作。

 

第一、从线程的第一个函数(本线程开始执行的函数)返回

/*************************************************************************
	> File Name: thread_exit_1.c
	> Author: laiyehua
	> Mail: ok.4444@qq.com
	> Created Time: Wed 01 Jan 2014 03:37:53 PM CST
 ************************************************************************/

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

void *thread_function(void *arg)
{
    pthread_detach(pthread_self());//将线程设置为分离
    printf("thread_function runing\n");

    /************************************************
     * 从线程第一个函数(本线程开始执行的函数)返回
     * 那么线程就完成了它的使命了,结束了
     ***********************************************/
}

int main(void)
{
    int ret;
    pthread_t tid;

    printf("init ok\n");

    /*创建一个线程*/
    ret = pthread_create(&tid, NULL, thread_function, NULL);
    if(ret != 0)
    {
        //如果创建线程失败,退出
        printf("pthread_create error\n");
        exit(EXIT_FAILURE);
    }

    printf("main end\n");
    /*退出main主线程*/
    pthread_exit(NULL);
}

执行结果:
init ok
main end
thread_function runing

 

第二,调用pthread_exit()函数

pthread_exit - terminate calling thread

void pthread_exit(void *retval);

/*************************************************************************
	> File Name: thread_create.c
	> Author: laiyehua
	> Mail: ok.4444@qq.com
	> Created Time: Sun 29 Dec 2013 11:00:32 AM CST
 ************************************************************************/

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

/**************************************************************
 * 函数功能:显示线程相应函数执行结果
 * 参数一:返回结果
 * 参数二:执行函数
 *************************************************************/
void disp_state_message(int ret, char *msg)
{
    /*线程函数基本上返回0表示成功*/
    if(ret != 0)
    {
        printf("%s error\n", msg);
        exit(EXIT_FAILURE);
    }
}

/****************************************************************
 * 函数功能:新创建的线程开始执行的函数
 * 参数一:线程创建时,传递过来参数
 * 返回:线程退出时,返回的值
 ***************************************************************/
void *thread_function(void *arg)
{
    printf("thread_function runing..............\n");
    printf("%s\n", (char *)arg);
    pthread_exit("hi!");//本线程执行完这个函数后,就结束了,不会再执行下面代码

    printf("======================\n");//这是没有被执行的
}

int main(void)
{
    int ret;//返回结果
    pthread_t tid;//创建线程ID号
    void *result;


    ret = pthread_create(&tid, NULL, thread_function, "hello thread_function!");//新创建一个线程,给线程传递参数"hello thread_function!"
    disp_state_message(ret, "pthread_create");//显示创建结果

    printf("main runing..............\n");

    //usleep(1000);//让出本线程,让其它线程有机会执行

    ret = pthread_join(tid, &result);//等待线程结果,如果创建的线程不是分离线程,应该使用这个函数回收线程的资源
    disp_state_message(ret, "pthread_join");
    printf("result:%s\n", (char *)result);


    exit(EXIT_SUCCESS);

}

 

执行结果:
main runing..............
thread_function runing..............
hello thread_function!
result:hi!

 

注意:在main()函数的主线程调用pthread_exit()函数,只是结束了主线程,其它线程还是会继续执行的,直到所有线程都结束了,本进程才会结束。

第三、调用exit()函数

在程序任何地方执行exit()函数,执行的程序,都会被马上终止。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值