考虑一下
#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/
最佳答案
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/