473,395 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

waiting for another thread without blocking resources...

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
Feb 14 '08 #1
6 5783
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 WaitForSomeCondition() 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."
Feb 14 '08 #2
On Feb 14, 10:59*am, Lars Uffmann <a...@nurfuerspam.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
Feb 14 '08 #3
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 WaitForSomeCondition() 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
Feb 14 '08 #4
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
Feb 14 '08 #5
Cholo Lennon wrote:
PS: Could you provide some code to check the library use?
boost::thread *THREAD_FileReceiver; // 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_FileReceiver = new boost::thread(&listenForUDPFiles);

// wait for thread to set GLB_Listening := 1 - how??

// the following is the cmd button to toggle thread status
mainWindow->cmdToggleListen->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_FileReceiver->join();
cout << "thread finished, GLB_listening = " << GLB_listening << endl;
delete THREAD_FileReceiver;
THREAD_FileReceiver = 0;

// the following is the cmd button to toggle thread status
mainWindow->cmdToggleListen->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
Feb 15 '08 #6
On Feb 15, 10:58 am, Lars Uffmann <a...@nurfuerspam.dewrote:
Cholo Lennon wrote:
PS: Could you provide some code to check the library use?

boost::thread *THREAD_FileReceiver; // 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_FileReceiver = new boost::thread(&listenForUDPFiles);

// wait for thread to set GLB_Listening := 1 - how??

// the following is the cmd button to toggle thread status
mainWindow->cmdToggleListen->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_FileReceiver->join();
cout << "thread finished, GLB_listening = " << GLB_listening << endl;
delete THREAD_FileReceiver;
THREAD_FileReceiver = 0;

// the following is the cmd button to toggle thread status
mainWindow->cmdToggleListen->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_lock(GLB_mutex);

// your code ...
}

Also, try locking global variable access in your thread function.

void listenForUDPFiles()
{
...

// Access shared flags (GLB_listen, GLB_listening)
{
boost::scoped_lock 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
Feb 15 '08 #7

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

Similar topics

9
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) ...
44
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...
0
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...
20
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...
12
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...
16
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...
7
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
20
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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,...

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.