In an event routine, I want to end a certain thread. I am setting a flag
that is checked by the thread and causes it to end, when it is set. Then
the thread sets a "response" flag, just before exiting. In the event
routine, I would like to wait for that response flag, because at that
point, I can be sure that the old thread (even if it still is alive
between setting the response flag and exiting) will no longer interfere
with a subsequent call.
However, a
while (!responseflag) ;
heavily blocks system resources and apparently also the read call blocks
the writing of the responseflag - I get caught in an endless loop there.
while (!responseflag) cout << "." << endl;
seems to do the job, with a random amount of periods printed to stdout,
but I wouldn't rely on it always working. So what is the thing to do
within that while loop?
TIA!
Lars 6 5799
Lars Uffmann schrieb:
In an event routine, I want to end a certain thread. I am setting a flag
that is checked by the thread and causes it to end, when it is set. Then
the thread sets a "response" flag, just before exiting. In the event
routine, I would like to wait for that response flag, because at that
point, I can be sure that the old thread (even if it still is alive
between setting the response flag and exiting) will no longer interfere
with a subsequent call.
However, a
while (!responseflag) ;
heavily blocks system resources and apparently also the read call blocks
the writing of the responseflag - I get caught in an endless loop there.
If you want to wait in a thread, you should not use a loop, but simply
wait. There might be some functions like WaitForSomeCond ition() in your
threading library, and you should ask this question in a newsgroup about
your platform or threading library.
--
Thomas http://www.netmeister.org/news/learn2quote.html
"Some folks are wise, and some otherwise."
On Feb 14, 10:59*am, Lars Uffmann <a...@nurfuersp am.dewrote:
while (!responseflag) ;
heavily blocks system resources and ...
It can improve you situation a little bit, if you release the
processor while waiting, e.g.:
while (!responseflag) sched_yield();
Of course, the actual instruction for yielding depends on what library
you are using.
However, you might consider using a semaphore for event signaling.
Best Regards,
Szabolcs
Thomas J. Gritzan wrote:
If you want to wait in a thread, you should not use a loop, but simply
wait. There might be some functions like WaitForSomeCond ition() in your
threading library, and you should ask this question in a newsgroup about
your platform or threading library.
boost only has a mailing list :/
And the thing is, actually my event procedure is not a thread. I was
hoping there was some way to have a button that starts and ends a thread:
First Click:
-set keepalive flag, start thread, thread set's a "i'm running" flag,
then checks keepalive flag every cycle
Second Click:
-unset keepalive flag, wait for "i'm running" flag to be un-set,
thread ends upon next check, unsets "i'm running"
For that I only have 1 thread, and the event procedure cannot be a
thread, nor should it be. There's got to be some c++ sleep function that
gives the thread some time to terminate, without blocking resources...
Or isn't there?
*confused*
Lars
Hi Yannick,
Thank you very much for your extensive reply - that seems to be shedding
some light on threading for me, but since I'll need some time to do what
you suggested, I wanted to post a quick reply first :)
Best Regards,
Lars
Cholo Lennon wrote:
PS: Could you provide some code to check the library use?
boost::thread *THREAD_FileRec eiver; // global variable
int GLB_listen = 0; // global
int GLB_listening = 0; // global
// event handler function
void OnToggleListen( )
{
if (!GLB_listen) {
if (GLB_listening) {
cout << "error: still ending thread" << endl;
return;
}
GLB_listen = 1; // set keepalive flag for thread
cout << "starting thread" << endl;
// this function is the threads main loop, receiving UDP packets
THREAD_FileRece iver = new boost::thread(& listenForUDPFil es);
// wait for thread to set GLB_Listening := 1 - how??
// the following is the cmd button to toggle thread status
mainWindow->cmdToggleListe n->SetLabel ("Stop Listening");
}
else {
if (!GLB_listening ) {
cout << "error: still starting thread" << endl;
return;
}
GLB_listen = 0; // unset keepalive flag for thread
// send a UDP packet to get thread out of listening mode
sendEndOfStream ();
cout << "waiting for thread to end" << endl;
THREAD_FileRece iver->join();
cout << "thread finished, GLB_listening = " << GLB_listening << endl;
delete THREAD_FileRece iver;
THREAD_FileRece iver = 0;
// the following is the cmd button to toggle thread status
mainWindow->cmdToggleListe n->SetLabel ("Start Listening");
}
}
---
this code just crashes after the join() call while without it, the code
would exit normally. However, then I have a possible race condition when
re-activating the thread. Are you able to make anything from that?
Thanks,
Lars
On Feb 15, 10:58 am, Lars Uffmann <a...@nurfuersp am.dewrote:
Cholo Lennon wrote:
PS: Could you provide some code to check the library use?
boost::thread *THREAD_FileRec eiver; // global variable
int GLB_listen = 0; // global
int GLB_listening = 0; // global
// event handler function
void OnToggleListen( )
{
if (!GLB_listen) {
if (GLB_listening) {
cout << "error: still ending thread" << endl;
return;
}
GLB_listen = 1; // set keepalive flag for thread
cout << "starting thread" << endl;
// this function is the threads main loop, receiving UDP packets
THREAD_FileRece iver = new boost::thread(& listenForUDPFil es);
// wait for thread to set GLB_Listening := 1 - how??
// the following is the cmd button to toggle thread status
mainWindow->cmdToggleListe n->SetLabel ("Stop Listening");
}
else {
if (!GLB_listening ) {
cout << "error: still starting thread" << endl;
return;
}
GLB_listen = 0; // unset keepalive flag for thread
// send a UDP packet to get thread out of listening mode
sendEndOfStream ();
cout << "waiting for thread to end" << endl;
THREAD_FileRece iver->join();
cout << "thread finished, GLB_listening = " << GLB_listening << endl;
delete THREAD_FileRece iver;
THREAD_FileRece iver = 0;
// the following is the cmd button to toggle thread status
mainWindow->cmdToggleListe n->SetLabel ("Start Listening");
}
}
---
this code just crashes after the join() call while without it, the code
would exit normally. However, then I have a possible race condition when
re-activating the thread. Are you able to make anything from that?
Yes, it's possible that you have a race condition. Try locking the
section. Global variables aren't good, but using the same scheme:
....
boost::mutex GLB_mutex;
void OnToggleListen( )
{
boost::scoped_l ock(GLB_mutex);
// your code ...
}
Also, try locking global variable access in your thread function.
void listenForUDPFil es()
{
...
// Access shared flags (GLB_listen, GLB_listening)
{
boost::scoped_l ock sl(GLB_mutex);
// update flags here
}
...
}
TODO: 1st Check if the solution work. 2nd Remove global variables and
optimize locking.
Regards
--
Cholo Lennon
Bs.As.
ARG This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Tom |
last post by:
I have created the following code for a product select/payment form (don't know if there is a better
way) and I have been trying to make the following changes (unsuccessfully so far):
1) Eliminate the submit button and submit the form with onchange.
2) Open the action php page in a new window.
I am using this code for different payment options (i.e., cc processing and paypal). As such, there
are multiple forms on the page. The...
|
by: Charles Law |
last post by:
Hi guys. I'm back on the threading gig again.
It's the age-old question about waiting for something to happen without
wasting time doing it.
Take two threads: the main thread and a worker thread. The worker thread is
reading the serial port, waiting for something to happen (a service
request). When it does it raises an event. Of course, the event is executed
on the worker thread. The idea is that when the event is raised, the handler...
|
by: Stephen Barrett |
last post by:
After reading through the many many many posts on threading, I still don't
quite understand how to accomplish a task.
Here is an overview of the app I am coding. I am sorry if it is a little
long.
A Windows Service app (no probs here) that basically needs to look for work
to do from 1 to many MSMQ queues that it then needs to pass on to 1 to many
(configurable) worker threads to process. The worker thread actually runs a
few...
|
by: Charles Law |
last post by:
Consider the following scenario:
A data packet is sent out of a serial port and a return packet is expected a
short time later.
The application sending the packet needs to send another packet as soon as
the return packet has been received. It needs to wait for the return packet,
but time out if one is not received.
Whilst packets are being exchanged the application needs to be responsive.
|
by: Raymond Lewallen |
last post by:
How to wait for a process to stop completion is my goal. Obviously, the
looping while waiting for the HasExited property is not a solution.. but
thats the best I can come up off the top of my head. Natuarally it will not
work. I expect it to use up all resources looping, which will not allow the
process to complete. The process takes about 60 seconds, because the .bat
file it is calling is rebuilding mulitple .NET solutions and projects...
| |
by: Bruce Wood |
last post by:
Maybe it's just late in my day, but I'm reading Jon's article on
threading, in particular how to use Monitor.Wait() and Monitor.Pulse(),
and there's something that's not sinking in. The code in question
looks like this:
public class ProducerConsumer
{
readonly object listLock = new object();
Queue queue = new Queue();
|
by: Bob |
last post by:
Process.start("Mydoc.doc") starts Word with the file. I need to wait for
Word to be closed before more code can execute in my app. How can I do this?
Thanks for any help
Bob
|
by: =?ISO-8859-1?Q?Gerhard_H=E4ring?= |
last post by:
John Dohn wrote:
When I do this, I put a special value in the queue (like None) and in
the worker thread, check for the special value and exit if found.
Threads can also be marked as "daemon threads" (see docs for
threading.Thread objects). This will make the application terminate if
only "daemon threads" are left.
So best would probably be soemthing like
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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...
|
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...
| |
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |