Multiple TMutex and R__LOCKGUARD

I have a program involving three TThread objects (thread1, thread2, thread3) and two TMutex objects (mutex12, mutex23). I would like to make thread1 and thread2 mutually exclusive using mutex1. Also, I would like to make thread2 and thread3 mutually exclusive using mutex23. My implementation is something like:

[code]//Thread1
{
R__LOCKGUARD(mutex12);
Commands(…);
}

//Thread2
{
R__LOCKGUARD(mutex12);
R__LOCKGUARD(mutex23);
MoreCommands(…);
}

//Thread3
{
R__LOCKGUARD(mutex23);
OtherCommands(…);
}[/code]

However, this will not compile. Is there a way to implement a scenario like this?

Does TMutex::Lock() pause the thread it’s in, if the mutex is already locked? If this is the case, I know the answer to my own question, but the documentation in the class index doesn’t specify if this is the case.

Thanks!

Hi,

R__LOCKGUARD(mutex) macro expands to:

TLockGuard R__guard(mutex)

so putting two in a row will cause an error about R__guard already existing. If you want to go this way do something like:

{
R__LOCKGUARD(mutex12);
{
R__LOCKGUARD(mutex23);
MoreCommands();
}
}

However, the more mutexes the more you serialize your code. Better try to solve the problem with as few mutexes as possible.

Cheers, Fons.