Is C ++ std :: queue safe if there is a stream for pushing only and only popping-only?

I wonder if this situation is (thread-) safe or not.

There is a thread that only pushes into std :: queue.
And there is another thread that appears only from std :: queue.
Since the queue is empty or not, it is controlled thread-safe, a later thread will not fail.

could you help me?

Thanks.

+4
source share
1 answer

I think the answer is no.

The standard says (ยง23.2.2 / 1):

To avoid data calculations (17.6.5.9), implementations should take into account the following functions: begin, end, rbegin, rend, front, back, data, find, lower_bound, upper_bound, equal_range, at, and, in addition to associative or unordered associative containers, the [ ].

At least, how I would interpret things (and Herb Sutter seems to agree ), which means that these functions (and only those functions) can be considered as "thread safe". Push and pop are not on the list, so you cannot assume that they are thread safe.

I would also add that even popping will actually write data - when you pull an item from the container, the size of the container must be updated, so when you click and press, entries appear. When more than one thread writes, you need to do something to ensure only one thread at a time.

To summarize: sorry, but no.

+9
source

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


All Articles