c++ - 在分离线程中执行 - 终止进程(c++ std 11)

标签 c++ multithreading exec

考虑一下

#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <vector>
#include <string>
#include <iterator>
#include <cmath>
#include <thread>
#include <cerrno>
#include <cstring>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>

#include <boost/algorithm/string.hpp>

#include "utils.cpp"
#include "pstream.h" 
//pstream is the process stream library as below
void doThings()
{
while(not exitflag, a global){
if(some condition) {execl (a bash script); std:cout << "this text will never get printed, process ends at execl" <<endl;}
}}}

int main()
{//detach  doThings as a thread here

}

我可以保证没有设置全局 exitflag,而且,即使设置了标志,也会打印 std::cout 行,因为没有命令 break 并跳转到 while 的末尾来庆祝在打印消息之前终止。

我做错了什么?顺便说一句:shell 脚本运行良好。

编辑:(感谢 minitech)我不想让原来的程序死掉

多线程:http://sourceforge.net/projects/pstreams/files/pstreams/Release%200.8.x/

最佳答案

来自 execl's documentation

The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2). (See the manual page for execve(2) for further details about the replacement of the current process image.)

您应该始终调用fork如果您不希望原始程序终止,请在调用 execl 之前。

关于c++ - 在分离线程中执行 - 终止进程(c++ std 11),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22085276/

相关文章:

c++ - 未定义对弱函数的引用(静态库+ GCC)

c++ - 是否可以在 "const"函数中更改成员变量的值?

c++ - 绑定(bind)到枚举的类成员

java - 在线程之间共享资源,不同 java 版本中的不同行为

SQL Server 字符串执行带有输出参数的过程

c - 在 child 返回之前获得返回状态而不会卡住

c++ - 提取运算符到非数字字符

c++ - 单例和多线程

c - 如何使用多线程丢弃 scanf 错误并从 4 个输入整数中识别最高素数

ruby-on-rails - 如何抑制使用 fork/exec 创建的子进程的任何输出