简体   繁体   中英

how to share a mutex lock for a group of pthreads ?

I'm trying to do some stuff with pthreads and sync them:
How could I use mutex just for a group of threads ?
Let's say I have t0,t1, t2, .. t20. pthreads running at the same time, and I want to have a lock for the even numbers threads and other lock for the odd numbers threads... or one lock for the first ten, and other for the rest, or one lock for each one. I mean, grouping pthreads depending on its data (the fourth argument in this funcion:

int pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

and sharing the mutex for a group of pthreads.

I'm working on a kind of bank project and I want to lock all the phreads trying to access to a same account number. (as critical section CRUD operations)
Does it make sense ? or there is a better approach to do this ?
Thanks in advance for your help and time ;)

J.

Mutual exclusion semaphores are not meant to be tied to specific threads, they're meant to protect specific resources.

In your case, that resource is the bank account. I'm not convinced that a mutex-per-account solution is a viable one, especially if your bank has tens of millions of customers, like some of the Chinese ones do :-)

A more workable approach may be to keep a list of the accounts currently being worked on in memory and use a single utex to protect that. The operations would then lock the mutex, check and possibly modify the list, then unlock the mutex.

The better approach that you're looking for is to use an ACID-type backing store (like a database) to ensure all updates are atomic.

It's up to you which threads share a given mutex. If you want a mutex only shared by the even-numbered threads, then all you have to do is ensure that that mutex is only accessed by the even-numbered threads.

Having said that, the right way to use a mutex is normally to associate it with a given set of data not a particular set of code. In your account number case, you could create one mutex for each account object, and ensure that any thread that accesses an account first locks the corresponding mutex (and unlocks it when it is done).

In general you don't lock threads - you lock data structures, where any/all threads that want to access that data structure has to acquire the data structure's mutex.

For your case, this means one mutex per bank account.

The next problem is that some things need to access multiple data structures. An example of this would be transferring $100 from one account to another (where you want to lock both accounts, then reduce the balance of the first account and increase the balance of the second account, then release both locks). This can lead to deadlocks. For example, if one thread wants to lock A then B, and another thread wants to lock B then A; then the first thread might lock A and the second thread might lock B, and then neither thread will be able to get the second lock that they need.

The solution to that problem is to have a global "lock order" and only acquire locks in that order.

For your case you have account numbers. If a thread wants to lock 2 or more accounts, then it determines which accounts, then sorts the list of accounts (eg from lowest account number to highest account number), then acquires all the locks it needs in that order. Once the thread has acquired all the locks it needs, it does whatever it has to do, then releases all the locks it acquired in the reverse order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM