简体   繁体   English

消息队列是否支持多线程?

[英]Does message queue support Multi-thread?

I have 3 questions about thread and process communication. 我有3个关于线程和进程通信的问题。

  1. Can the Linux function msgget(), msgsnd(), and msgrcv() be invoked by multiple threads in one process? 可以在一个进程中由多个线程调用Linux函数msgget(),msgsnd()和msgrcv()吗? These functions in different threads are to attempt to access(r/w) one process' message queue. 不同线程中的这些函数将尝试访问(r / w)一个进程的消息队列。 Are all race conditions supposed to be taken care by the system? 所有种族条件都应该由系统来处理吗? If not, is there any good method to support threads and send a message to its main thread(process)? 如果没有,是否有任何好的方法来支持线程并向其主线程(进程)发送消息?

  2. Can semop() function be used to synchronize threads in one process? semop()函数可以用于在一个进程中同步线程吗?

  3. There is a shared memory which have the following entities to access. 有一个共享内存,可以访问以下实体。

    • process 处理
    • several threads in one process. 一个进程中的几个线程。 Do I have to use semaphore of inter-process level and a semaphore of threads level at the same time ? 我必须在同一时间使用进程间水平的信号量和线程级信号灯? Any simple way to handle this? 有什么简单的方法可以处理吗?

A lot of question. 很多问题。 :) thanks. :) 谢谢。

Can the Linux function msgget(), msgsnd(), and msgrcv() be invoked by multiple threads in one process? 可以在一个进程中由多个线程调用Linux函数msgget(),msgsnd()和msgrcv()吗?

You do not need to worry about race conditions, the system will take care of that, there is no race condition with these calls. 您无需担心竞争条件,系统会处理这些问题,这些呼叫没有竞争条件。

can semop() function be used to synchronize threads in one process? 可以使用semop()函数在一个进程中同步线程吗?

Yes, read more in the documentation 是的,请阅读文档中的更多内容

Do I have to use semaphore of inter-process level and a semaphore of threads level? 我是否必须使用进程间级别的信号量和线程级别的信号量?

Any resource which is shared globally among threads or processes is subject to race conditions due to one or more threads or processes trying to access it at the very same time, So you need to synchronize the access to such a shared global resource. 由于一个或多个线程或进程在同一时间尝试访问它,因此线程或进程之间全局共享的任何资源都会受到竞争条件的影响,因此您需要同步对这种共享全局资源的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM