Home > Back-end >  Excuse me STD: : queue write scenarios in a read a thread safe?
Excuse me STD: : queue write scenarios in a read a thread safe?

Time:12-09

I have A thread A only responsible for the push, another thread B only responsible for judging whether is empty, if not null, then pick up front, and then pop, could you tell me the application, will it be A problem? What are the problems?

CodePudding user response:

The STL library is thread-safe, need to lock or choose thread-safe or lock-free queue

CodePudding user response:

If the queue involves data changes, multithreading is locked,

CodePudding user response:

Ask, I this scenario above, where is the problem?

CodePudding user response:

STL is thread-safe

CodePudding user response:

Thank you teacher, I understand this is thread-safe, but I just want to know, I this scenario application, there will be a problem? I wonder if I can avoid this scenario, the unsafe situation?

CodePudding user response:

Push pop will obviously to add or delete adapter the underlying container elements, may even for memory capacity and pointer modification, will modify the value of a size, it need under a pile of processing in a multi-threaded is atomic operation, otherwise, they may be exceptions, such as vector, internal have a pointer to a heap of array with a size and a capacity, when you push, increases an element, but also change the size, the size is too big to can't hold, will trigger memory capacity, the overall copy, release the original memory, modify the capacity for a heap of operation, these are not atomic, they only want to be locked in a multithreaded,

CodePudding user response:

I can feel safe, but I couldn't say a reason,

CodePudding user response:

STL not thread-safe, also means that more than one thread used at the same time, the lock will be needed.
Your a scenario, write a read depends on the specific container type, such as you use the vector.
When you read a thread to get the data ready to read, write a thread to push, but found that the vector space is not enough, will redistribute space, and release the original space.
Is when you read a thread to get the address of data is "wild" pointer, then the program crash naturally.

Also, when data change STL containers, iterators will also fail.

CodePudding user response:

In the pursuit of high performance environment, can be used to the realization of circular queue unlocked thread safe queue, but the STL not by default, the future possible thread-safe

CodePudding user response:

Push the operation is not atomic, if performed in the operating process thread, another thread is incomplete data
  • Related