Effective Synchronization: Multiple Read, Single Write
-
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
-
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
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_
-
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
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++ -
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_
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
-
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
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_
-
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
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.
Mark Salsbery Microsoft MVP - Visual C++ :java: