Click here to Skip to main content
15,882,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Can someone help me achieve thread - safe code of the following code: without using mutex or semaphores or synchronous block.

Locking and Unlocking of Resources arranged in the form of n-ary Tree - GeeksforGeeks[^]

Calling lock or unlock from different threads and it should still work fine.

What I have tried:

I can only came up with solution using mutex.
Updated 22-Feb-22 11:20am

The web page already has some suggested solutions, so follow those.
Share this answer
CPallini 12-Oct-21 5:53am    
5.A pragmatic approach.
Richard MacCutchan 12-Oct-21 6:16am    
Member 15390900 12-Oct-21 6:08am    
I want to call lock() and unlock() from various different threads and it should still work fine , that is not written there in web-page
Richard MacCutchan 12-Oct-21 6:14am    
Fine, so figure out where in the code you need to put the locks and give it a try.
Member 15390900 12-Oct-21 6:18am    
here by putting the locks you mean mutex locks?
You could use an atomic[^] rather than a mutex. That's the only thing I can think of that's faster.

The entire tree must be locked because it's in a transient state until all of the ancestors have been updated.

When using an atomic, a thread that fails to lock a resource simply fails to obtain it. The benefit of a mutex is that the thread gets scheduled out by the operating system until the resource becomes available. Here, the resource is the entire tree, which means that if a thread is currently updating the tree, another thread will fail to obtain the resources associated with a node even if those resources are currently available.

In C++20, it is possibly to wait on an atomic, but the cost will be much like using a mutex. I'm assuming the use of exchange instead.

The code on the page you linked would also fail my code inspection:
o In C++, use nullptr instead of NULL.
o Replace if (a == true) and if (a == false) with if (a) and if (!a).
o Remove flag and replace if (T->isLock = true)... by if (T->isLock) return;
Share this answer
Joe Woodbury 12-Oct-21 20:10pm    
"The code on the page you linked would also fail my code inspection:"

You're being too kind. At quick glance, in addition to what you wrote:

Not using member initializers,
Not using const.
Not making data private.
Naming a variable 'T'
The use of raw 'new'
If actually used outside of 'main' code leaks
Greg Utas 12-Oct-21 20:42pm    
A Nietszche quote about flyswatters comes to mind. :)
Joe Woodbury 12-Oct-21 20:46pm    
the purpose of using mutex and semaphores are sharing a resource between threads and or processes. In multithread application without using mutex or semaphores one can not reserve that resource into the thread first getting permission and entering critical session, other threads or proseses will be suspended by OS. Just after releasing mutex other ones in order will get permission to use resource locking mutex and resource and so on...
It must be in this way at least i know only this way.
Good luck,
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