A simple C ++ container class that is thread safe for writing

I am writing a multi-threaded program using OpenMP in C ++. At some point, my program plugs into many threads, each of which must add “jobs” to some container that tracks all added jobs. Each task can be a pointer to an object.

Basically, I just need to add pointers to some container from several threads at the same time.

Is there a simple solution that works well? After some searches, I found that STL containers are not thread safe. Some qaru.site/questions/1030118 / ... consider this question, but none of them had consensus on a simple solution.

+3
source share
2 answers

There is no built-in way to do this. You can simply use a lock to protect one of the existing container types. Perhaps it would be better if each thread used its own container and then combined the results at the end.

+7
source

Using a mutex or similar synchronization primitive to control access to a linked list is not very difficult, so I would recommend that you try first.

, , : . , , . , ; .

+1

Source: https://habr.com/ru/post/1742163/


All Articles