Difference between Critical Sections over Mutex objects
-
Hi all, I have seen two difference, 1. Critical Sections are relatively faster than Mutex. That is synchronizing with the same process, is more efficient. 2. Mutex could be used with cross-process thread synchronization. So I have two questions, 1. Am I correct? 2. Any other points on this? Your comments are really appreciate.
I appreciate your help all the time... CodingLover :)
-
Hi all, I have seen two difference, 1. Critical Sections are relatively faster than Mutex. That is synchronizing with the same process, is more efficient. 2. Mutex could be used with cross-process thread synchronization. So I have two questions, 1. Am I correct? 2. Any other points on this? Your comments are really appreciate.
I appreciate your help all the time... CodingLover :)
From the book Programming Applications for Microsoft Windows by Jeffrey Richter, Mutexes behave identically to critical sections, but mutexes are kernel objects, while critical sections are user-mode objects. This means that mutexes are slower than critical sections. But it also means that threads in different processes can access a single mutex,... I think this line answers your two questions.
-
From the book Programming Applications for Microsoft Windows by Jeffrey Richter, Mutexes behave identically to critical sections, but mutexes are kernel objects, while critical sections are user-mode objects. This means that mutexes are slower than critical sections. But it also means that threads in different processes can access a single mutex,... I think this line answers your two questions.
Thanks for the comment. Yeah your comment give me a some kind of idea. But then, what is the advantage use of a mutex to control multiple process and multiple Critical Sections to control multiple process?
I appreciate your help all the time... CodingLover :)
-
Hi all, I have seen two difference, 1. Critical Sections are relatively faster than Mutex. That is synchronizing with the same process, is more efficient. 2. Mutex could be used with cross-process thread synchronization. So I have two questions, 1. Am I correct? 2. Any other points on this? Your comments are really appreciate.
I appreciate your help all the time... CodingLover :)
Mutex and Critical Section are used for synchronization.Both are almost same except that Critical Section is used within a same process area, where mutex is used in different process area. I will explain you with simple example: Suppose in your project, You are using one array and two threads are running simultaneously. First thread add data in an array and other display that data on screen. Scenario 1: Without Critical section There is a schedular program in OS that give time slot to every process and threads that are running. It is so frequently done that we feel that all threads are running simultaneous.Suppose time slot given to both thread is equal. If thread one is running it can add only 40 data and than time slot is given to other thread and it display 50 data. Now for some data some garbage data is displayed. If threads uses the same resourse and we must synchronize the thread. In this case we use CS.Process area of both the thread is same, So it is fast. Scenario 2: Suppose we use printer machine to print some data. If two person request at a time and if no synchronization process is used than it gives to garbage data. At this time one process have to wait to complete the process of second. This time the process or thread are from different process area. So we use mutexes. The process area of process is different, So it is slow. I think this example will clear your doubt. :)
"Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."
-
Mutex and Critical Section are used for synchronization.Both are almost same except that Critical Section is used within a same process area, where mutex is used in different process area. I will explain you with simple example: Suppose in your project, You are using one array and two threads are running simultaneously. First thread add data in an array and other display that data on screen. Scenario 1: Without Critical section There is a schedular program in OS that give time slot to every process and threads that are running. It is so frequently done that we feel that all threads are running simultaneous.Suppose time slot given to both thread is equal. If thread one is running it can add only 40 data and than time slot is given to other thread and it display 50 data. Now for some data some garbage data is displayed. If threads uses the same resourse and we must synchronize the thread. In this case we use CS.Process area of both the thread is same, So it is fast. Scenario 2: Suppose we use printer machine to print some data. If two person request at a time and if no synchronization process is used than it gives to garbage data. At this time one process have to wait to complete the process of second. This time the process or thread are from different process area. So we use mutexes. The process area of process is different, So it is slow. I think this example will clear your doubt. :)
"Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."
Thanks for the comment. Pretty much clear what you are saying now. However lets consider the scenario 1 for a minute. As you explain we can use CS to control the two threads and guaranteed that data adding or reading take place at a time. But the order of the execution is not guaranteed. That's clear. At the same time we can mutex on that too. At that point what's the advantage of each. Hope I am clear with my this question.
I appreciate your help all the time... CodingLover :)
-
Thanks for the comment. Pretty much clear what you are saying now. However lets consider the scenario 1 for a minute. As you explain we can use CS to control the two threads and guaranteed that data adding or reading take place at a time. But the order of the execution is not guaranteed. That's clear. At the same time we can mutex on that too. At that point what's the advantage of each. Hope I am clear with my this question.
I appreciate your help all the time... CodingLover :)
My suggestion is you must read about the synchronization of thread. There are four types of synchronization 1) Critical section 2) Mutex 3) Event 4) Semaphore Every synchronization object is used to meet different requirement and as per our requirement we have to choose which synchronization object is used in our application.
"Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."
-
My suggestion is you must read about the synchronization of thread. There are four types of synchronization 1) Critical section 2) Mutex 3) Event 4) Semaphore Every synchronization object is used to meet different requirement and as per our requirement we have to choose which synchronization object is used in our application.
"Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."
SP 24 wrote:
My suggestion is you must read about the synchronization of thread.
Of course, I myself have to do that. :) BTW do you know any good online resources? Regarding my previous comment, do you have any thoughts of it? If so I would like to know.
I appreciate your help all the time... CodingLover :)
-
SP 24 wrote:
My suggestion is you must read about the synchronization of thread.
Of course, I myself have to do that. :) BTW do you know any good online resources? Regarding my previous comment, do you have any thoughts of it? If so I would like to know.
I appreciate your help all the time... CodingLover :)
Google will help you :) MSDN a) MultiThreading[^] b) Multithreading: When to Use the Synchronization Classes[^] c) Informit[^]
"Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."
-
Google will help you :) MSDN a) MultiThreading[^] b) Multithreading: When to Use the Synchronization Classes[^] c) Informit[^]
"Every Little Smile can touch Somebody's Heart... May we find Hundreds of Reasons to Smile Everyday... and May WE be the Reason for someone else to smile always!" (ICAN) "Your thoughts are the architects of your destiny."
Thanks a lot. I'll have a look at them. :)
I appreciate your help all the time... CodingLover :)