Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ pthread - How to make map access threadsafe?

I have a map as member variable and multiple threads that access the map (read and write access). Now I have to ensure that only ONE thread have access to the map. But how do I dot that? What is the best solution for that?

like image 647
Tobi Weißhaar Avatar asked Oct 17 '25 18:10

Tobi Weißhaar


1 Answers

Boost contains some nice lock implementations for shared access. Have a look at the documentation.

In your case you probably need a read-write lock because a mutual exclusion lock is probably overkill if you have a lot of reads and very few writes.

like image 161
Tudor Avatar answered Oct 20 '25 08:10

Tudor