Boost logo

Boost :

From: Kowalke Oliver (QD IT PA AS) (Oliver.Kowalke_at_[hidden])
Date: 2008-02-01 07:21:40

what's the best practice in following scenario:
m threads and n resources (each resource is protected by a mutex)
thread x needs read/write access to resources (...i,j,k,..)
threads y,z,... do access another subset of resources at the same time

inorder to protected deadlocks I would do following in the code of the different threads (code executed by the threads are different):

try_lock: // label
unique_lock< mutex > lk1( mtx1, try_to_lock_t);
unique_lock< mutex > lk4( mtx2, try_to_lock_t);
unique_lock< mutex > lk7( mtx3, try_to_lock_t);
if ( ! ( lk1 && lk2 && lk3) ) goto try_lock;
// all mutexes are locked
// now modify safely resource 1,2,3

Maybe you have a better pattern do solve this problem?

best regards,

Boost list run by bdawes at, gregod at, cpdaniel at, john at