Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Difference between Critical Sections over Mutex objects

Difference between Critical Sections over Mutex objects

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodingLover
    wrote on last edited by
    #1

    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 :)

    V S 2 Replies Last reply
    0
    • C 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 :)

      V Offline
      V Offline
      venkatmakam
      wrote on last edited by
      #2

      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.

      http://www.mono-project.com/Main\_Page

      C 1 Reply Last reply
      0
      • V venkatmakam

        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.

        http://www.mono-project.com/Main\_Page

        C Offline
        C Offline
        CodingLover
        wrote on last edited by
        #3

        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 :)

        1 Reply Last reply
        0
        • C 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 :)

          S Offline
          S Offline
          ShilpiP
          wrote on last edited by
          #4

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

          C 1 Reply Last reply
          0
          • S ShilpiP

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

            C Offline
            C Offline
            CodingLover
            wrote on last edited by
            #5

            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 :)

            S 1 Reply Last reply
            0
            • C 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 :)

              S Offline
              S Offline
              ShilpiP
              wrote on last edited by
              #6

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

              C 1 Reply Last reply
              0
              • S ShilpiP

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

                C Offline
                C Offline
                CodingLover
                wrote on last edited by
                #7

                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 :)

                S 1 Reply Last reply
                0
                • C 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 :)

                  S Offline
                  S Offline
                  ShilpiP
                  wrote on last edited by
                  #8

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

                  C 1 Reply Last reply
                  0
                  • S ShilpiP

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

                    C Offline
                    C Offline
                    CodingLover
                    wrote on last edited by
                    #9

                    Thanks a lot. I'll have a look at them. :)

                    I appreciate your help all the time... CodingLover :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups