Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Okay I am working on a multithreaded application. I dont have any code to provide as I havent started it. Im trying to find out how to sync data access. It is going to be a data tree. The tree can be read by multiple threads at the same time. But as soon as a thread need to delete a node all read access need to be locked until the delete is complete. And only one thread can delete a node at a time. From all my research I have found ways to allow one thread access at a time for read or delete through use of mutex. But I havent found a way to allow multiple reads at the same time while locking reads while deleting. If someone could just point me in a good direction. To a good tutorial that would help me greatly. Thanks
Posted
Comments
Sergey Alexandrovich Kryukov 22-Feb-12 20:27pm    
OS?
--SA

You could use:
ACE_RW_Thread_Mutex[^] or boost::shared_mutex[^]

Boost homepage[^]
Ace homepage[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Feb-12 20:28pm    
Sure, a 5.
--SA
Espen Harlinn 23-Feb-12 5:51am    
Thank you Sergey!
Ashish Tyagi 40 23-Feb-12 0:23am    
Perfect!!!!!!!!! +5ved
Espen Harlinn 23-Feb-12 5:51am    
Thank you Ashish!
Sergey Alexandrovich Kryukov 9-Mar-12 12:47pm    
Hi Espen,

Can I ask you to advise on a somewhat related question?
Please take a look at this question:

http://www.codeproject.com/Questions/343545/Starting-a-thread-from-a-thread-within-a-class

Please also take a look on other posts on the page, including my answer.

OP got a very right idea but she messed up things a bit ;-). The question in C++, and I actually answered in full, but my most advanced samples are mine, and only for .NET. Nevertheless, the recipe is given. The idea is to use instance method instead of the static method (required by _beginthreadex) with the work-around: passing "this" additionally. This static method can call the instance method (which becomes a "true" thread method) explicitly, using passed instance pointer. Pretty simple. But my further step is making well-encapsulated thread wrapper which I demonstrated only for .NET.

I would expect that at least one of the C++ libraries you know does exactly this: we could find out a ready-to use C++ base class encapsulating the thread and providing good object-oriented thread interface for derived classes. I would write such thing by myself, but a ready-to-use library would be really nice. Am I right? If you could find such class or template class, could advise on that? If you could find out and post this as another answer, it would be a perfect one.

If you do, please notify me as well; I would be interested to take a look.

How about that?
--SA
What you have is the classic "Multiple Readers / Single Writer Lock" problem. Lots of people have written articles and solutions to this.

Bing Search[^] will reveal many sources of information and algorithms to implement that.
 
Share this answer
 
From all my research I have found ways to allow one thread access at a time for read or delete through use of mutex. But I havent found a way to allow multiple reads at the same time while locking reads while deleting.

Reading the node would never need sync-ing.
It's only when you try to delete or modify the node. If it's within process you could use critical section to remove nodes from the tree.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900