Std :: vector, thread safety, multithreading

I use std :: vector as shared data in a multi-threaded application. I encapsulate the stream inside the class, for example,

class ABC { public: double a, b, c; }; boost::mutex mutex1; class XYZ { public: XYZ(vector<ABC> & pVector) { ptrVector = &pVector; m_thread = boost::thread(&XYZ::Start, this); } ~XYZ() {} void Start(); public: vector<ABC> * ptrVector; boost::thread m_thread; }; void XYZ::Start() { try { while(1) { boost::this_thread::interruption_point(); for (unsigned int i=0; i<ptrVector->size(); i++) { { boost::mutex::scoped_lock lock(mutex1); ptrVector->at(i).a = double(rand())/10000; ptrVector->at(i).b = double(rand())/10000; ptrVector->at(i).c = double(rand())/10000; } } } } catch(boost::thread_interrupted) {} catch(std::exception) {} } 

When I close the application, sometimes there will be 2 error messages in the debug file, sometimes there will be no error messages. I often heard people say that std :: vector are not thread safe, is this one of the cases? I am using Visual Studio 2008, increasing the flow, the size of the vector is fixed. Someone can also offer some tips on how to use std :: vector in a multi-threaded application.

  • First chance exception in 0x7688b9bc in ETP.exe: Microsoft C ++ Exception: std :: out_of_range in memory location 0x02d8f7bc ..
  • The exception for the first chance at 0x00e916e0 in ETP.exe is: 0xC0000005: access read location with errors 0x00000008.
  • Second chance error: file c: \ program files (x86) \ microsoft visual studio 9.0 \ vc \ include \ vector, Line Second Chance Assertion Failed: file c: \ program files (x86) \ microsoft visual studio 9.0 \ vc \ include \ vector98

Thanks.

+6
source share
3 answers

In fact, it is completely pointless to indicate X whether or not it is thread safe! You have to pick up for what kinds of uses. For example, it is unlikely that any class will be "thread safe" when it is somehow used in one thread and destroyed in another.

However, the statement that std::vector<T> not thread safe, regardless of how often it repeats, is false . However, it seems that most people do not understand and do not appreciate the provided guarantees of thread safety. std::vector<T> is thread safe in the following sense:

  • You can read a vector object from several streams at the same time.
  • If there is one thread modifying a vector object, there should be neither simultaneous readers, nor writers.
  • Access to the vector object does not interfere with other vector objects.

This applies to the vector structure itself. Access to the contained object is tied to any rules imposed on them. This, apparently, is not a guarantee of thread safety, which many people have in mind, but something stronger will not work with the container interface.

+22
source

You call ptrVector->size() without first closing it. This may be the cause of your problems. Before recording or recording, be sure to lock your vector.

+4
source

Maybe you could use this instead?

concurrent_vector ... from Intel Threading Building blocks

https://software.intel.com/en-us/node/467758

0
source

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


All Articles