在main线程中调用pthread_exit会起到只让main线程退出,但是保留进程资源,供其他由main创建的线程使用,直至所有线程都结束,但在其他线程中不会有这种效果

https://stackoverflow.com/questions/3559463/is-it-ok-to-call-pthread-exit-from-main

To allow other threads to continue execution, the main thread should terminate by calling pthread_exit() rather than exit(3).

It's fine to use pthread_exit in main. When pthread_exit is used, the main thread will stop executing and will remain in zombie(defunct) status until all other threads exit.

If you are using pthread_exit in main thread, cannot get return status of other threads and cannot do clean-up for other threads (could be done using pthread_join(3)). Also, it's better to detach threads(pthread_detach(3)) so that thread resources are automatically released on thread termination. The shared resources will not be released until all threads exit.

在win10的wls中,使用 g++ -g -pthread pthreadtest.cpp编译如下代码,然后执行,会发现main退出之后,pthread1和pthread2还是会继续跑。而,如果在mian中,注释pthread_exit(NULL);,使用return退出,则pthread1和pthread2也会随着退出。

#include <pthread.h>
#include <iostream>
#include <unistd.h>
using namespace std;
//int a = 1, b = 2;
void * fun(void * param){
int *ptr = (int *)param;
while(true){
cout <<"from pthread " << (*ptr) << endl;
sleep(2);
}
return NULL;
} int main(){
int a = 1, b = 2;
pthread_t t1, t2;
pthread_create(&t1, NULL, fun, &a);
pthread_create(&t2, NULL, fun, &b);
int c = 10;
while(--c > 0){
cout << "from main " << c << endl;
sleep(1);
}
cout << "main pthread_exit \n";
pthread_exit(NULL);
cout << "main return \n";
return 0;
}

使用pthread_exit(NULL);, 输出结果如下:

pthread_exit在main线程中的用处的更多相关文章

  1. JAVA 线程中的异常捕获

    在java多线程程序中,所有线程都不允许抛出未捕获的checked exception(比如sleep时的InterruptedException),也就是说各个线程需要自己把自己的checked e ...

  2. java 多线程 :ThreadLocal 共享变量多线程不同值方案;InheritableThreadLocal变量子线程中自定义值,孙线程可继承

      ThreadLocal类的使用 变量值的共享可以使用public static变量的形式,所有的线程都是用同一个public static变量.如果想实现每一个线程都有自己的值.该变量可通过Thr ...

  3. java main()线程是不是最后一个退出的(相比较main中创建的其他多个线程)

    JVM会在所有的非守护线程(用户线程)执行完毕后退出: main线程是用户线程: 仅有main线程一个用户线程执行完毕,不能决定JVM是否退出,也即是说main线程并不一定是最后一个退出的线程. pu ...

  4. iOS 报错:(子线程中更新UI)This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

    今天在写程序的时候,使用Xcode 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了. **ProjectName[1512:778965] This application ...

  5. C#中的线程(中)-线程同步

    1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具:                       简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...

  6. 线程创建,属性设置与获得,等待线程结束,线程中fork,以及执行exec()

    这篇博客的形式我想以分析代码不同情况为主: 点击(此处)折叠或打开 #include<stdio.h> #include<pthread.h> #include<time ...

  7. 在Main方法中设置异常的最后一次捕捉

    在做Winfrom程序时,有时会遇到一个异常,可是这个异常不知道在什么地方发生的,程序会自动关闭,然后什么也没有了,在网上找到了一种方法,用来捕捉这种异常. 出现这种情况的原因是在程序中某些地方考虑不 ...

  8. 为什么说invalidate()不能直接在线程中调用

      1.为什么说invalidate()不能直接在线程中调用?2.它是怎么违背单线程的?3.Android ui为什么说不是线程安全的?4.android ui操作为什么一定要在UI线程中执行? 1. ...

  9. 在线程中调用SaveFileDialog

    在多线程编程中,有时候可能需要在单独线程中执行某些操作.例如,调用SaveFileDialog类保存文件.首先,我们在Main方法中创建了一个新线程,并将其指向要执行的委托SaveFileAsyn.在 ...

随机推荐

  1. PHP 图片base64 互转

    <?php /* http://tool.css-js.com/base64.html 透明图片 <img src="data:image/jpg;base64,iVBORw0K ...

  2. [Swift]LeetCode290. 单词模式 | Word Pattern

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  3. [Swift]LeetCode874. 模拟行走机器人 | Walking Robot Simulation

    A robot on an infinite grid starts at point (0, 0) and faces north.  The robot can receive one of th ...

  4. Java货币金额转换为大写形式

    package com.test; import java.math.BigDecimal; /** * * * 数字转换为汉语中人民币的大写<br> * */ public class ...

  5. Nginx学习系列一搭建环境

    1.Win10下安装vmware14虚拟机软件 官方下载地址 全程next,输入key,激活即可. 2.在虚拟机中安装Linux服务器环境,操作系统为Centos7 继续下一步,安装完成! 3.下载C ...

  6. 在.NET中使用Redis

    dll文件 namespace RedisDemo { public partial class RedisPage : System.Web.UI.Page { protected void Pag ...

  7. C++可变参数模板实现输出

    C++11 tuple&可变参数模板 template void Print(T value) { std::cout << value << std::endl; } ...

  8. 『最短Hamilton路径 状态压缩DP』

    状压DP入门 最短Hamilton路径 Description 给定一张 n(n≤20) 个点的带权无向图,点从 0~n-1 标号,求起点 0 到终点 n-1 的最短Hamilton路径. Hamil ...

  9. Java 多线程(五)—— 线程池基础 之 FutureTask源码解析

    FutureTask是一个支持取消行为的异步任务执行器.该类实现了Future接口的方法. 如: 取消任务执行 查询任务是否执行完成 获取任务执行结果(”get“任务必须得执行完成才能获取结果,否则会 ...

  10. 模板模式之clone()方法

    clone()方法的使用,体现了模板模式的思想,常见用法可以参考:http://blog.csdn.net/zhangjg_blog/article/details/18369201