ImpactCorey
Programmer
This question goes out to you POSIX thread experts.
I have two threads set up, one for the GUI, one for a DAQ server. The DAQ server aquires the data and updates the visible GUI screen every 1/2 second (loop with a sleep in it) and calls my Controller::update_gui function. The GUI will switch the screen whenever certain radio buttons are pressed. The function for this is Controller::switch_gui.
I set up a mutex lock around the two functions so the insides won't be executed at the same time. i.e. the GUI should not be updated if the screen is changing. You can see it is not important for the gui to always update. It merely tries to lock the mutex but if it is unsuccessful just exits the function. The GUI change must always occur. I figured having condition waits are unnecessary. (I don't even know how I would implement them other than on the update_gui function but like I said, it isn't necessary to run every time it is called). The problem is, I get deadlocks.
Here is my code: Does anybody see anything wrong with it?
// update the gui_list
int Controller::update_gui( int iUpdateType )
{
if( pthread_mutex_trylock( &mutex ) != 0 )
return -1;
// loop through all dialogs in map and update each one
notify( iUpdateType );
pthread_mutex_unlock( &mutex );
return 0;
}
int Controller::switch_gui( int iOldID, int iNewID, ObserverDlg * dlg )
{
pthread_mutex_lock( &mutex );
// remove dialog at key "iOldID"
detach( iOldID );
// add dialog "dlg" at key "iNewID"
attach( iNewID, dlg );
pthread_mutex_unlock( &mutex );
return 0;
}
I have two threads set up, one for the GUI, one for a DAQ server. The DAQ server aquires the data and updates the visible GUI screen every 1/2 second (loop with a sleep in it) and calls my Controller::update_gui function. The GUI will switch the screen whenever certain radio buttons are pressed. The function for this is Controller::switch_gui.
I set up a mutex lock around the two functions so the insides won't be executed at the same time. i.e. the GUI should not be updated if the screen is changing. You can see it is not important for the gui to always update. It merely tries to lock the mutex but if it is unsuccessful just exits the function. The GUI change must always occur. I figured having condition waits are unnecessary. (I don't even know how I would implement them other than on the update_gui function but like I said, it isn't necessary to run every time it is called). The problem is, I get deadlocks.
Here is my code: Does anybody see anything wrong with it?
// update the gui_list
int Controller::update_gui( int iUpdateType )
{
if( pthread_mutex_trylock( &mutex ) != 0 )
return -1;
// loop through all dialogs in map and update each one
notify( iUpdateType );
pthread_mutex_unlock( &mutex );
return 0;
}
int Controller::switch_gui( int iOldID, int iNewID, ObserverDlg * dlg )
{
pthread_mutex_lock( &mutex );
// remove dialog at key "iOldID"
detach( iOldID );
// add dialog "dlg" at key "iNewID"
attach( iNewID, dlg );
pthread_mutex_unlock( &mutex );
return 0;
}