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. Effective Synchronization: Multiple Read, Single Write

Effective Synchronization: Multiple Read, Single Write

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsontutorialquestion
6 Posts 4 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.
  • S Offline
    S Offline
    singh_nav
    wrote on last edited by
    #1

    I am implementing multi-threaded application in C++ in which I have 2 functions i.e. 1. Read 2. Write and both are sharing one global variable. There are 10 threads,1 thread will use write function and rest 9 will use read function.When writing is taking place, all thread should wait else remaining 9 threads should not wait to read. Now my question is how to use the synchronization technique so that 9 threads should not wait if no writing is taking place. Thanks

    _ C M 3 Replies Last reply
    0
    • S singh_nav

      I am implementing multi-threaded application in C++ in which I have 2 functions i.e. 1. Read 2. Write and both are sharing one global variable. There are 10 threads,1 thread will use write function and rest 9 will use read function.When writing is taking place, all thread should wait else remaining 9 threads should not wait to read. Now my question is how to use the synchronization technique so that 9 threads should not wait if no writing is taking place. Thanks

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #2

      Use CSemaphore with CSingLock/CMultiLock. You can call a lock on the object/resource while writing is in progress

      Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      S 1 Reply Last reply
      0
      • S singh_nav

        I am implementing multi-threaded application in C++ in which I have 2 functions i.e. 1. Read 2. Write and both are sharing one global variable. There are 10 threads,1 thread will use write function and rest 9 will use read function.When writing is taking place, all thread should wait else remaining 9 threads should not wait to read. Now my question is how to use the synchronization technique so that 9 threads should not wait if no writing is taking place. Thanks

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        In such scenario (one writer, multiple readers), you can avoid using critical section using guards 'around' your variable. Suppose you use two integers, m_iSeqId1 and m_iSeqId2 (both initialized to 0). Your writer thread will first increase m_iSeqId1, then update the new value, then increase m_iSeqId2 (so that it becomes equal to m_iSeqId1). Your reader threads first read m_iSeqId2 (yes m_iSeqId2 and not m_iSeqId1), read the value and then read m_iSeqId1. If both sequence Ids are the same, the value can be read and is not corrupted. If the Ids are different, the read should repeat the full operation until both Ids are the same. But, this is only usefull when you are working with variables that cannot be set in one single instruction (e.g. an array, a string, an object, ...). Assignements of simple types (like int, char, ...) are atomic.

        Cédric Moonen Software developer
        Charting control [v1.5] OpenGL game tutorial in C++

        1 Reply Last reply
        0
        • _ _AnsHUMAN_

          Use CSemaphore with CSingLock/CMultiLock. You can call a lock on the object/resource while writing is in progress

          Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

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

          But Semaphore is use to limit the number of thread, where as my requirement is different. I don't want to limit my number of read and write thread. Actually i want that if any writing there is in progress then other all thread ( read and write) must wait. where as any number of reading thread can run concurrently. I believe that this can be archived by WaitForMultipleObject. but how to use it. Any link for the same or any hint. Thanks

          _ 1 Reply Last reply
          0
          • S singh_nav

            But Semaphore is use to limit the number of thread, where as my requirement is different. I don't want to limit my number of read and write thread. Actually i want that if any writing there is in progress then other all thread ( read and write) must wait. where as any number of reading thread can run concurrently. I believe that this can be archived by WaitForMultipleObject. but how to use it. Any link for the same or any hint. Thanks

            _ Offline
            _ Offline
            _AnsHUMAN_
            wrote on last edited by
            #5

            Semaphore does allow you to access the resources when the threads are limited, and is not used to limit the number of threads. MSDN - ***An object of class CSemaphore represents a "semaphore" — a synchronization object that allows a limited number of threads in one or more processes to access a resource If you want to use WaitForMultipleObject than you can store the handles of the thread in an array and then pass this as a parameter to it.

            Some things seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

            1 Reply Last reply
            0
            • S singh_nav

              I am implementing multi-threaded application in C++ in which I have 2 functions i.e. 1. Read 2. Write and both are sharing one global variable. There are 10 threads,1 thread will use write function and rest 9 will use read function.When writing is taking place, all thread should wait else remaining 9 threads should not wait to read. Now my question is how to use the synchronization technique so that 9 threads should not wait if no writing is taking place. Thanks

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              singh_nav wrote:

              Now my question is how to use the synchronization technique so that 9 threads should not wait if no writing is taking place.

              Reader Writer Lock[^]

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              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