Reputation: 21
I have a strange issue with vectors, I initialize a class member vector in the main thread and then call a thread which try to access the front() of that vector. But accessing the vector front causes a runtime error
Here is code for main thread (dispatchQueue is a private class member of class Engine)
dispatchQueue.push_back(TempObj);
boost::thread processThread(&Engine::initializeExecutorService, this);
processThread.start_thread();
processThread.join();
And the code for member function initializeExecutorService is as follows (processingQueue is a private class member)
while (nextIterationAvailable) {
if (pendingProcess) {
processingQueue.push_back(dispatchQueue.front());
dispatchQueue.pop_back();
}
}
The initializeExecutorService works fine if I call it with main thread
UPDATE
The Debugger reports
Signal received: SIGSEGV (Segmentation fault) For program postmaster-cpp-ng-obj, pid 13,614 You may discard the signal or forward it and you may continue or pause the process
When I try to run with Netbeans it reports
RUN FINISHED; Segmentation fault; core dumped;
Running with gdb shows
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff68e8700 (LWP 13821)]
[New Thread 0x7ffff60e7700 (LWP 13822)]
[New Thread 0x7ffff58e6700 (LWP 13823)]
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff60e7700 (LWP 13822)]
0x00007ffff79cba1b in ?? () from /usr/lib/libboost_thread.so.1.54.0
Upvotes: 0
Views: 979
Reputation: 1362
In addition to Danvil's response, I would like to add a note. May be this could help, as once I have faced similar issue in a multithreaded application.
You should also check whether dispatchQueue state is valid or not. In our application another thread was deleting the vector and it was crashing below code snippet.
if(myvector.size)
We have used Windbg to diagnose the issue.
Upvotes: 0
Reputation: 22991
std::vector::front
fails when the vector is empty.
This is a safe version:
// lock dispatchQueue
if(!dispatchQueue.empty()) {
processingQueue.push_back(dispatchQueue.front());
dispatchQueue.pop_back();
}
// unlock dispatchQueue
Additionally using the first element and removing the last looks dubious.
Upvotes: 1