yoni
yoni

Reputation: 1364

What are the differences between locking mechanisms for multi-thread and multi-proccess applications?

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.

Upvotes: 0

Views: 536

Answers (4)

shaydel
shaydel

Reputation: 589

  1. 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)

  2. 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 .

  3. 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.

  4. since threads (for the same process) have the same memory they can synchronize on variables\lock objects on User level

  5. 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

Upvotes: -1

Tudor
Tudor

Reputation: 62459

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.

Upvotes: 2

Roman Ryltsov
Roman Ryltsov

Reputation: 69706

So, my question is what about the case that the operation system decide to divide the theards to different process, or even different processors.

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.

Upvotes: 4

Drew Dormann
Drew Dormann

Reputation: 63849

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.

Upvotes: 4

Related Questions