【发布时间】:2019-01-09 21:54:36
【问题描述】:
我编写了一个程序,我在主线程中创建一个线程并使用system() 从线程启动另一个进程。此外,我也在主函数中使用system() 启动相同的过程。即使父进程死亡,从线程启动的进程似乎仍然活着。但是从主函数调用的函数与父函数一起死亡。任何想法为什么会发生这种情况。
请在下面找到代码结构:
void *thread_func(void *arg)
{
system(command.c_str());
}
int main()
{
pthread_create(&thread_id, NULL, thread_func, NULL);
....
system(command.c_str());
while (true)
{
....
}
pthread_join(thread_id, NULL);
return 0;
}
【问题讨论】:
-
您对
thread_func()的定义不正确 - 它缺少参数。请参阅link,其中显示了一个示例。 -
@kiner_shah 这只是一个骨架。我的实际代码中确实有这个东西
标签: c++ multithreading pthreads fork system-calls