I have a multithreaded application which has a linked list. A lot of places in the code are traversing the linked list, but there is ONE place where nodes are removed. The places where the linked list is traversed need not be protected from each other, ie, they can happen parallelly but the place where the removal happens needs to happen when none of the other traversing places are in action. If I have a mutex and lock all places - both all the traversing places and the delete place, it works, but that would slow down the application as a whole. What I plan to do is, have a variable(semaphore) that would increment before the traversal starts and decrement it when the traversal ends. The increment and the decrement is protected by a mutex variable. The delete place would work by holding the mutex variable AND when the counting semaphore is 0(indicating nobody is traversing the linked list). Would this work? Is this the right way to design this situation? Are there better ways? Any help is appreciated.
TIA