I simulated my condition in the below, maybe will help find out where my fault lies
#include "../JuceLibraryCode/JuceHeader.h"
static int counter = 0;
class CSTrial : public Thread
{
public:
CSTrial()
:Thread("CS Trial " + String(++counter))
{
id = counter;
startThread(1);
}
~CSTrial()
{
signalThreadShouldExit();
waitForThreadToExit(-1);
}
//@override
void run()
{
const ScopedLock sl(cs);
doStuff();
sleep(500);
std::cerr << id << ": about to end run()\n";
}
void doStuff()
{
std::cerr << id << ": do Stuff\n";
}
CriticalSection cs;
int id;
};
//==============================================================================
int main (int argc, char* argv[])
{
// ..your code goes here!
CSTrial cst1;
CSTrial cst2;
return 0;
}
I used Introjucer to create this console project.
On Xcode debug mode, the print is ALWAYS:
JUCE v2.0.28
2: do Stuff
1: do Stuff
2: about to end run()
1: about to end run()
On Xocde release mode, the print could be:
2: do Stuff
1: do Stuff
21: about to end run()
: about to end run()
or
1: do Stuff
2: do Stuff
12: about to end run()
: about to end run()
or
2: do Stuff
1: do Stuff
2: about to end run()
1: about to end run()
In my real code, the doStuff counterpart is some loading file job which may take one second or two.
In any case of the above, the doStuff() is reached in the different CSTrial thread. Is it possible to make the lock work? Or I misunderstand some theoretical thing on mutex?
thx.