Reputation: 1329
I have a thread running as part of a program that I need to keep running through the duration of my program. However, as I run the program, I want to be able to periodically show what data is being processed on the thread (without stopping it).
Conceptually, is that possible and/or good practice? From what I have been reading about threads, it seems like in order to access a thread, you have put it to sleep, see what you want, and then start it again. Is there a way to "mirror" that thread? Even if I can do that, is that good practice to access a thread directly, or do I need to dump the information to a data structure first and then read it?
Extra Information:
I am not sure if this makes a difference, but I created a thread using the C++ CreateThread function. I was just reading in another post that there was a memory leak problem with that method. However, I thought that HOW I create the thread changes the level of the OS it is working on and might affect whether or not I was able to access the thread or not.
Thanks for any advice in advance!
Upvotes: 0
Views: 1174
Reputation: 3950
You can also use a mutex
to guard the variable before you read or write to it: Look at the following C++ Concept: Mutex
Also the Mutex class can be of value.
If it is just a counter or some basic type you can also consider looking at a atomic type.
As others have suggested, try to use std::thread if you have c++11.
For asynchronous notifications that can be used to send data from one thread to another thread look at the Qt library and their Signals and Slots mechanism.
Upvotes: 1
Reputation: 1787
I think sleep
is not an option.
Better solution is to use condition variable to organize a blocking queue like this.
And std::thread
would be a good choice for multi-threading.
So having all this, your main thread can push a message to the queue and your child thread (which is popping messages) will then use it.
Upvotes: 0