让我解释一下:我已经在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.
更多推荐
发布评论