SOLUTION: handling GUI resource locks between event handler and boost::thread
Hey Paavo,
I'll have to chew a bit on your example, doesn't seem so obvious to me -
I haven't yet learned even nearly all aspects of C++.
Put it in my "re-read" folder :)
Meanwhile, I managed to solve my locking problem with the help of input
from this thread, in the way stated below.
Best Regards & thanks to everyone involved,
Lars
solution relevant code excerpt (missing includes and wxWidgets
initialization):
/* *** */
debuggingGUImainFrame *mainWindow; // wxWidgets main window,
// controls cmd1 (wxButton) and txt1 (wxTextCtrl)
int glbSERVICING = 0; // "keepalive flag" for the background thread
// mutex to ensure alternate access to window controls and glbSERVICING
boost::mutex mutexChecka;
typedef boost::mutex::scoped_lock lockaChecka;
void demonstrateFrameLock()
{
sleep (1);
cout << "child thread: acquiring lock" << endl;
lockaChecka lc (mutexChecka);
cout << "child thread: got lock, child thread: ready for servicing"
<< endl;
mainWindow->cmd1->Enable();
int i = 0;
while (glbSERVICING) {
lc.unlock(); // unlock r/w access to keepalive flag
cout << "child thread: unlocked" << endl;
++i;
sleep (2);
cout << "child thread: servicing... (i = " << i << ")" << endl;
lc.lock(); // prepare read access to keepalive flag
cout << "child thread: got lock" << endl;
}
cout << "child thread: mainWindow->txt1->GetValue = " <<
mainWindow->txt1->GetValue() << endl;
mainWindow->cmd1->Enable();
lc.unlock();
cout << "child thread: unlocked, thread finished" << endl;
}
void debuggingGUImainFrame::OnToggle( wxCommandEvent& event )
{
cmd1->Disable();
cout << "main thread: acquiring lock" << endl;
lockaChecka lc (mutexChecka);
cout << "main thread: got lock" << endl;
if (glbSERVICING) {
glbSERVICING = 0;
mainWindow->cmd1->SetLabel (wxT ("Enable Service"));
}
else {
glbSERVICING = 1;
mainWindow->cmd1->SetLabel (wxT ("Disable Service"));
boost::thread *helperThread;
helperThread = new boost::thread(&demonstrateFrameLock);
delete helperThread;
helperThread = 0;
}
cout << "main thread: unlocking" << endl;
lc.unlock();
cout << "main thread: thread finished" << endl;
}
/* *** */