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. Create a thread & use mutex for synchronization

Create a thread & use mutex for synchronization

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 5 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.
  • R Offline
    R Offline
    Rsh
    wrote on last edited by
    #1

    I want to create two threads. One thread should write to the CLidt object and the other thread should read from the CList object.And i want to use Mutex as the synchronization object, so that only one thread can access the CList object at a time. Pls. help Rsh

    M 1 Reply Last reply
    0
    • R Rsh

      I want to create two threads. One thread should write to the CLidt object and the other thread should read from the CList object.And i want to use Mutex as the synchronization object, so that only one thread can access the CList object at a time. Pls. help Rsh

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      Actually a critical section is a better sync object for your purposes, and easier to use. Each thread calls EnterCriticalSection() before accessing the list, then LeaveCriticalSection() once it's done with the list. --Mike-- Just released - RightClick-Encrypt - Adds fast & easy file encryption to Explorer Like the Google toolbar? Then check out UltraBar, with more features & customizable search engines! My really out-of-date homepage Sonork-100.19012 Acid_Helm

      W 1 Reply Last reply
      0
      • M Michael Dunn

        Actually a critical section is a better sync object for your purposes, and easier to use. Each thread calls EnterCriticalSection() before accessing the list, then LeaveCriticalSection() once it's done with the list. --Mike-- Just released - RightClick-Encrypt - Adds fast & easy file encryption to Explorer Like the Google toolbar? Then check out UltraBar, with more features & customizable search engines! My really out-of-date homepage Sonork-100.19012 Acid_Helm

        W Offline
        W Offline
        wilche
        wrote on last edited by
        #3

        Just out of interest, why is critical section better in this case? Wouldn't mutex serve the same purpose? Regards, wilche

        P T 2 Replies Last reply
        0
        • W wilche

          Just out of interest, why is critical section better in this case? Wouldn't mutex serve the same purpose? Regards, wilche

          P Offline
          P Offline
          paulb
          wrote on last edited by
          #4

          A critical section is more lightweight than a mutex. A mutex differs from a critical section in it can be accessed across process boundaries and you can specify a timeout when waiting on it. Neither of these are needed in his situation so might as well use a critical section.

          1 Reply Last reply
          0
          • W wilche

            Just out of interest, why is critical section better in this case? Wouldn't mutex serve the same purpose? Regards, wilche

            T Offline
            T Offline
            Tim Smith
            wrote on last edited by
            #5

            The technical reason is that a critical section doesn't require a switch to kernel mode unless a second thread is trying to lock it when another already has the lock. A mutex will always cause a kernel mode switch. Tim Smith I know what you're thinking punk, you're thinking did he spell check this document? Well, to tell you the truth I kinda forgot myself in all this excitement. But being this here's CodeProject, the most powerful forums in the world and would blow your head clean off, you've got to ask yourself one question, Do I feel lucky? Well do ya punk?

            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