3 ms·
Same here. You just stick to a few simple patterns that work. If you have a shared data structure you protect that with a mutex. You don't hold the mutex and
by YZF 9y ago
Same here. You just stick to a few simple patterns that work. If you have a shared data structure you protect that with a mutex. You don't hold the mutex and call other functions. You take the mutex, you read or write the shared structure, and you release the mutex. That's the basic pattern.
People run into deadlocks and other issues when they don't realize what mutex protects which data, or they try to take multiple mutexes, or they hold a mutex while calling a callback, or they don't protect access at all.
From a performance perspective you need to be aware of how much contention you have on a mutex. Typically locking a mutex should never block. If you have contention there are patterns for dealing with that as well.
For most types of concurrent applications you're done. Yeah, lockless techniques, spin locks, etc. may have their place but for the most part keep it simple.