从线内分叉是否安全?(Is it safe to fork from within a thread?)

编程入门 行业动态 更新时间:2024-10-26 08:29:59
从线内分叉是否安全?(Is it safe to fork from within a thread?)

让我解释一下:我已经在Linux上开发了一个应用程序,它分叉和执行一个外部二进制文件并等待它完成。 结果由fork +进程独有的shm文件传递。 整个代码被封装在一个类中。

现在我正在考虑线程化以加快速度。 有许多不同的类函数实例可以并发地(使用不同的参数)分离和执行二进制文件,并将结果与​​它们自己的独特shm文件进行通信。

这个线程安全吗? 如果我在一个线程内分叉,除了安全之外,还有什么我需要注意的吗? 任何意见或帮助非常感谢!

Let me explain: I have already been developing an application on Linux which forks and execs an external binary and waits for it to finish. Results are communicated by shm files that are unique to the fork + process. The entire code is encapsulated within a class.

Now I am considering threading the process in order to speed things up. Having many different instances of class functions fork and execute the binary concurrently (with different parameters) and communicate results with their own unique shm files.

Is this thread safe? If I fork within a thread, apart from being safe, is there something I have to watch for? Any advice or help is much appreciated!

最满意答案

fork ,即使有线,是安全的。 一旦你分叉,线程是独立的每个进程。 (也就是说,线程正交于分叉)。 但是,如果不同进程中的线程使用相同的共享内存进行通信,则必须设计一个同步机制。

forking, even with threads, is safe. Once you fork, the threads are independent per process. (That is, threading is orthogonal to forking). However, if the threads in different processes use the same shared memory to comunicate, you have to devise a synchronization mechanism.

更多推荐

本文发布于:2023-08-06 20:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:safe   fork   thread

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!