Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronize between many threads. Only one is writing

Process during its lifetime has 1 main thread and from 1 to 50 other worker threads. When main thread accepts new connection it saves it in boost::unordered_map, lets call it "new con container". Worker threads time to time check "new con container" for new connections. For sync purposes there is one boost::mutex. When main thread writes to "new con container" it locks this mutex. Worker threads while checking this container also lock the mutex.

Is there way for worker threads not to lock the mutex and thread-safely read from "new con container" ? Worker threads do real-time operations on sockets, so locking mutex beats on performance.

like image 328
Didar_Uranov Avatar asked Jan 24 '26 11:01

Didar_Uranov


1 Answers

If you have a single writer and multiple readers consider using shared_mutex:

The class boost::shared_mutex provides an implementation of a multiple-reader / single-writer mutex.

like image 183
Tudor Avatar answered Jan 25 '26 23:01

Tudor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!