3

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.

Didar_Uranov
  • 1,230
  • 11
  • 26
  • How does a worker thread decide which new connection should be processed, and how does the next worker thread know not to work on the same connection? – jxh Jul 04 '12 at 08:10
  • goog: many threads reading –  May 19 '13 at 17:09

2 Answers2

4

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.

Tudor
  • 61,523
  • 12
  • 102
  • 142
0

Instead of polling the "new con container" by the worker threads, you should use a condition variable to let the workers block until there is something for one of them to do. This will reduce contention and thus the overhead of the lock.

Additional you can partitioning the container. Instead of having one container, use more and assign a fixed bunch of threads to every container. The main thread will then push every new connection to a different container.

Kind regards Torsten

Torsten Robitzki
  • 3,041
  • 1
  • 21
  • 35