472,353 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

handling GUI resource locks between event handler and boost::thread

I have this wxWidgets OnButtonClick event handler, that apparently holds
a lock on all widgets in my form, but this event handler is supposed to
end a thread in the background - while that thread is supposed to update
widget content. So if I want to wait for the thread to end in the event
handler, I have 2 threads waiting on each other - with the one in the
background waiting to access the widget content.

My current workaround is to introduce a boost::mutex, have the
background thread acquire a lock on it upon start, release upon end, and
the OnButtonClick event handler will check (or try to acquire and
instantly release) the lock on that mutex to make sure the previous
background thread has ended before starting a new one.

The according code looks like this:

boost::mutex mutexChecka;
typedef boost::mutex::scoped_lock lockaChecka;

void demonstrateFrameLock()
{
cout << "child thread: acquiring lock" << endl;
lockaChecka lc (mutexChecka);
cout << "child thread: got lock" << endl;

sleep (1);
cout << "child thread: taking a nap (2 seconds)" << endl;
sleep (2);
cout << "child thread: woke up!" << endl;

cout << "child thread: mainWindow->txt1->GetValue = " <<
mainWindow->txt1->GetValue() << endl;

cout << "child thread: unlocking" << endl;
lc.unlock();
cout << "child thread: unlocked" << endl;

cout << "child thread: thread finished" << endl;
}

void debuggingGUImainFrame::OnToggle( wxCommandEvent& event )
{
cout << "main thread: acquiring lock" << endl;
lockaChecka lc (mutexChecka);
cout << "main thread: got lock, unlocking" << endl;
lc.unlock();
cout << "main thread: unlocked" << endl;
if (myThread) {
cout << "main thread: deleting old thread" << endl;
delete myThread;
myThread = 0;
}

myThread = new boost::thread(&demonstrateFrameLock);
cout << "main thread: sleeping 5 seconds" << endl;
sleep (5);
cout << "main thread: done sleeping" << endl;
// myThread.join(); // old code, never got this to work due to the lock
on form controls
cout << "main thread: thread finished" << endl;
}

This gets the job done, but it really doesn't seem very pretty to me -
so being a newbie with thread programming, I would appreciate any
comments on how to improve this code (which is, of course, incomplete
and just the sample that remained after long hours of debugging).

Thank you in advance!

Lars
Mar 11 '08 #1
3 2259
On 11 Mar, 16:01, Lars Uffmann <a...@nurfuerspam.dewrote:
I have this wxWidgets OnButtonClick event handler, that apparently holds
a lock on all widgets in my form, but this event handler is supposed to
<snip>
boost::mutex mutexChecka;
typedef boost::mutex::scoped_lock lockaChecka;
wxWidgets have a support forum at http://wxforum.shadonet.com/ and
boost have user support mailing lists at http://www.boost.org/more/mailing_lists.htm#users

HTH.
Mar 11 '08 #2
bytebro wrote:
wxWidgets have a support forum at http://wxforum.shadonet.com/ and
boost have user support mailing lists at http://www.boost.org/more/mailing_lists.htm#users
HTH.
No, not at all. You told me two things that I do already know, and btw.
there is no newsgroup where boost is more on-topic than this one,
because the boost mailing list - while having a newsgroup portal -
doesn't allow posting via the latter.

I guess I'll add a disclaimer in my signature saying which resource
sites I know and which I do not :) Otherwise yes, your information would
have been partially helpful - but a direct answer was what I was looking
for.

Thanks anyways,

Lars
Mar 12 '08 #3
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;
}
/* *** */
Mar 13 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dingounan | last post by:
how to compile boost.thread,and set it? I have trouble. platform: windows XP compiler: Borland C++ Compiler 5.5.1 commandline tools thanks.
1
by: deluded.soul | last post by:
Hi everyone, I am trying to use the boost multithreading library. I am having a problem as the join() function for the thread never returns. I am...
4
by: Lighter | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; using namespace boost; void hello() { cout << "Hello world, I'm...
5
by: linyanhung | last post by:
I used a boost multi thread in VS 2005 on a Duo Core PC, and made a two thread process. The code is something like this: #include...
3
by: Gary Wessle | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; class waiter { public: waiter(); void waiting(); void...
4
by: Gary Wessle | last post by:
Hi given the Boost thread example here http://www-eleves-isia.cma.fr/documentation/BoostDoc/boost_1_29_0/libs/thread/example/thread.cpp the...
2
by: Chameleon | last post by:
Why this strange output? Why so many d'tor calls? The code: ---------------------------------------------------------- #include <cstdio>...
2
by: Hans Mull | last post by:
Hi! I'm experimenting with boost::thread. I have a GUI application with a slot function: void someFunction(){...} void...
1
by: Boogie | last post by:
Hi, A specific problem arises when I try to use boost::thread 1.35.0 with Borland Turbo 2006. Code below compiles but when run it throws (in...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.