线程和进程同步的信号量和互斥量 [英] Semaphores and Mutex for Thread and Process Synchronization

查看:185
本文介绍了线程和进程同步的信号量和互斥量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在线程和进程级别使用信号量和互斥量感到困惑.我们可以在线程和进程同步中使用信号量和互斥量吗,还是在线程和进程级别上有不同的信号量和互斥量?我的问题是关于POSIX API的.

I am confused with the usage of semaphores and mutexes at thread and process level. Can we use semphores and mutexes for both thread and process synchronization, or do we have different semaphores and mutexes both at thread and process level? My question is with reference to the POSIX API's.

推荐答案

两个问题的答案都是肯定的.您可以将互斥量和信号量创建为进程共享或不共享的.因此,您可以将它们用作进程间或线程间同步对象,但是创建它们时必须指定它们.

The answer to both questions is yes. You can create both mutexes and semaphores as either process-shared or not. So you can use them as interprocess or interthread synchronization objects, but you have to specify which when you create them.

当然,您必须在希望访问它的所有上下文共享的内存中创建同步对象.使用线程,这很简单,因为它们共享内存视图.对于进程,您必须专门在共享内存中创建同步对象.

Of course, you must create the synchronization object in memory that is shared by all contexts that wish to access it. With threads, that's trivial since they share a view of memory. With processes, you have to create the synchronization object in shared memory specifically.

这篇关于线程和进程同步的信号量和互斥量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆