I have a program in C++ that runs a bunch of threads to manipulate the same data. Each of these threads have a pointer to an object that is being manipulated, for example:
thread1 and thread2 both have a pointer to object1
object1->addSomething() can be used by either thread1 or 2 and refer to the same object
Now, these operations might give some trouble if they are being done at the same moment by both threads, so I want a simple mechanism for blocking. What I want is simply this:
void method()
{
waitUntilFree()
blockForOthers()
doSomething()
unblock()
}
Is there a simple way to do this? I just want to block and wait until it is free. I don't mind that the thread might have to wait a while. Is there an easy mechanism to do this? I use Boost for these threads but I couldn't for the life of me figure out a way to do this (seemlingly) simple block-and-wait thing.