I have game and I have two threads , one generates custom class and needs to store that (I put to push that in queue but I am not sure if that is thread safe, first thread generates every 50ms new instance, and second can read faster if there is any or slower – speed changes over time) . Another thread uses if queue is not empty , pop first and calculates some things. Is there any data structure thread safe for this problem in stl or boost ?
Share
Using
std::queueor any similar container will not be thread safe. If you want your access (push/pop) to be thread-safe, while using std::queue, you should useboost::mutexor a similar mechanism to lock before each access. You can look atboost::shared_mutexif you need immutable reads from more than one thread (not sure you need that based on what you described).Apart from that, you can take a look at
boost::interprocess::message_queue, as someone has already mentioned -> http://www.boost.org/doc/libs/1_50_0/boost/interprocess/ipc/message_queue.hpp for the most recent version of boost.Moreover, there is the concept of lock-free queues en.wikipedia.org/wiki/Non-blocking_algorithm. I cannot provide an example of such implementation but I am sure you can find some if you google around.