多线程和多进程应用程序的锁定机制有什么区别?
我有一段代码可以处理多线程(使用共享资源)问题,如下所示:
CRITICAL_SECTION gCS;
InitializeCriticalSection(&gCS);
EnterCriticalSection(&gCS);
// Do some shared resources stuff
LeaveCriticalSection(&gCS);
在 这个 MSDN 页面 是这样写的:“单个进程的线程[我的粗体]可以使用用于互斥同步的关键部分对象。”
所以,我的问题是:操作系统决定将线程划分到不同的进程,甚至不同的处理器的情况怎么样?
EnterCriticalSection
确实不能完成这项工作吗?如果答案是“关键部分对多重处理没有帮助”,那么还有什么选择呢?
我不想使用 Boost 类。
I have a piece of code that handles the multi-threading (with shared resources) issue, like that:
CRITICAL_SECTION gCS;
InitializeCriticalSection(&gCS);
EnterCriticalSection(&gCS);
// Do some shared resources stuff
LeaveCriticalSection(&gCS);
In this MSDN page is written: "The threads of a single process [my bold] can use a critical section object for mutual-exclusion synchronization."
So, my question is: what about the case that the operating system decides to divide the threads to different processes, or even different processors.
Does EnterCriticalSection
indeed not do the job? And if the answer is "critical sections are no help with multi-processing", what is the alternative?
I prefer not to use the Boost classes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
操作系统不会将线程划分为不同的进程。
EnterCriticalSection
适用于具有多个线程的程序以及具有多个处理器的系统。An operating system will not divide a thread into different processes.
EnterCriticalSection
is appropriate for programs with multiple threads, as well as systems with multiple processors.不同的处理器 - 关键部分涵盖了这一点。
不同的进程 - 您需要不同的同步API,可以在进程之间共享[内核]对象,例如互斥体 和 信号量。
请参阅使用互斥对象中的示例用法 部分。
Different processors - critical sections cover this.
Different processes - you need different synchronization API, which can share [kernel] objects between processes, such as mutexes and semaphores.
See sample usage in Using Mutex Objects section.
如果所有线程都在同一个程序中启动,则它们是单个进程的一部分,并且任何人(包括操作系统)都无法“分离它们”。它们仅作为该过程的一部分而存在,并将随着该过程而消亡。使用关键部分是完全安全的。
If all your threads are started in the same program, they are part of a single process and there is nothing anyone, including the OS, can do to "separate them". They exist only as part of that process and will die with the process. You are perfectly safe using a critical section.
进程被分配了一个新的地址空间(栈和堆),而当一个线程被创建时,它被隐式地分配了发起进程的内存空间,但是对于新分配的自己的堆栈空间(一个新的堆栈空间被分配给每个不同的线程)
对于操作系统来说,线程的执行方式与进程相同,当然,当使用线程时,这可能会导致更多的缓存和内存\页面命中。
操作系统执行器将为进程提供时间,然后进程可以使用自己的调度程序在线程之间分配时间,但这不是必须的,因为所有线程都是进程,它们位于同一个进程表中,并且可以在任何核心上运行并发\在任何时候,与常规进程相同。
由于线程(对于同一进程)具有相同的内存,因此它们可以在用户级别上的变量\锁定对象上进行同步
进程不应该访问不同进程分配的内存(除非他是联合空间的线程),因此进程之间的同步应该在一些加入\全局空间或在内核级别
A process is been allocated a newly address space(stack&heap), whereas when a thread is created it is implicitly assigned the initiator process's memory space ,but for a newly allocated own stack space (a new stack space is assigned to each and every different thread)
for the OS a thread executes the same as it was a process,naturally when using threads this might result in more cache and memory\page hits .
the OS executer will give time to the process who then may use his own scheduler to divide time between his threads,but this is not a must since all threads are processes they are in the same process table and can run on any core concurrently\at any time, the same as regular process.
since threads (for the same process) have the same memory they can synchronize on variables\lock objects on User level
a process should not have access to a different process's allocated memory(unless he is a thread of joint space) so synchronizing between processes should be done on some joined\global space or at kernel level