消息队列支持多线程吗?

时间:2022-11-09 20:41:40

I have 3 questions about thread and process communication.

我有三个关于线程和进程通信的问题。

  1. Can the Linux function msgget(), msgsnd(), and msgrcv() be invoked by multiple threads in one process? These functions in different threads are to attempt to access(r/w) one process' message queue. 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)?

    一个进程中的多个线程可以调用Linux函数msgget()、msgsnd()和msgrcv()吗?在不同的线程中,这些函数尝试访问(r/w)一个进程的消息队列。所有的比赛条件都应该由系统来处理吗?如果没有,是否有任何好的方法来支持线程并向其主线程(进程)发送消息?

  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.

很多问题。:)谢谢。

1 个解决方案

#1


3  

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.

在线程或进程之间全局共享的任何资源都受到竞争条件的限制,因为一个或多个线程或进程试图同时访问它,因此需要同步对这种共享全局资源的访问。

#1


3  

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.

在线程或进程之间全局共享的任何资源都受到竞争条件的限制,因为一个或多个线程或进程试图同时访问它,因此需要同步对这种共享全局资源的访问。