2

Trying to decide weather to use asio strands?

My biggest need is for threads is accessing a Database with multiple reads and a single write.

Seems like strands only allow one thread at a time, is there some way to do multi read and one write using strands?

Why are strands better then just using mutex's?

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
Oliver K
  • 295
  • 2
  • 15

1 Answers1

0

boost strands serialize all activity that is given to them. A mutex is often used to ensure that one thread gets exclusive access to a resource.

The question mentions that you would like to access a database with multiple readers and one writer.

I would consider using a database connection pool design. A connection pool typically will let you configure how many connections are in the pool and it has acquire/release semantics. If a connection is not available the pool can return an error, wait until a connection is returned and becomes available, or increase the number of connections available.

One connection pool can be created for the readers. It can be configured to have many connections available so that multiple threads can each request a connection, use it, and then release it back to the pool.

A second connection pool can be used for the single writer connection. This pool should only allow one database connection (i.e. single writer). If the connection is in use by a thread the other threads can wait until the connection is released and the becomes available.

Extra care is always needed in a multi-threaded design. Be sure to take a close look at what/how each thread uses synchronized resources such as the pools described above.

skimobear
  • 1,188
  • 10
  • 12