Skip to content
Advertisement

C++ pThread program isn’t running to completion [closed]

I’m having an odd issue where my C++ multithreaded program doesn’t run to completion. Its supposed to run NUM_THREADS – 1 times. Any resources to help point me in the right direction would be appreciated! Currently these are the important functions

JavaScript

Advertisement

Answer

In the code above I see potential Undefined Behavior problem during bought variable access. This could be the reason of your problem.

In the while loop there is lines:

JavaScript

In void* processTransact(void* argument) function:

JavaScript

Code from processTransact modifies size of bought in separate thread. In the while loop access to bought.size() is not synchronized with use of mLock mutex. It means you could potentially get empty bought vector after bought.size() > 0 validation. It causes Undefined Behavior at rand() % 0.

Moreover, C++ standard doesn’t guarantee thread safety when same container (bought) modified and read from different threads.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement