473,770 Members | 5,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::s coped_lock lockaChecka;

void demonstrateFram eLock()
{
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 debuggingGUImai nFrame::OnToggl e( 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(& demonstrateFram eLock);
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 2377
On 11 Mar, 16:01, Lars Uffmann <a...@nurfuersp am.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::s coped_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) :
/* *** */

debuggingGUImai nFrame *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::s coped_lock lockaChecka;

void demonstrateFram eLock()
{
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 debuggingGUImai nFrame::OnToggl e( 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(& demonstrateFram eLock);
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
2818
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
7756
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 using a boolean variable to indicate when the thread should stop running. Here is basically a stub of what I am doing: Code:
4
2517
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 a thread!" << endl; }
5
2395
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 <boost/thread/thread.hpp> void fun1() { //do something
3
7271
by: Gary Wessle | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; class waiter { public: waiter(); void waiting(); void preform();
4
42630
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 code below attempts to run the example thread while giving the user a prompt for data input. I am failing to get the prompt while the thread is running, what am I doing wrong?
2
2334
by: Chameleon | last post by:
Why this strange output? Why so many d'tor calls? The code: ---------------------------------------------------------- #include <cstdio> #include <boost/thread/thread.hpp> class A {
2
3349
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 Frame::OnOkButtonClick(...) { switch(someInteger) { case 0: break
1
1937
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 commented line - thread creation): "assertion failed: p_ != 0, file .\boost/intrusive_ptr.hpp, line 126" (thread holds NULL pointer to thread function) I've tested this code under gcc 3.4 (mingw32) and gcc 3.2 (linux) - it worked fine in both cases.
0
10225
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.