0

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!

Community
  • 1
  • 1
JustBlossom
  • 1,259
  • 3
  • 24
  • 53
  • Normal approach would be for the thread to send an event somewhere. Without knowing your context, if you are working on files in a directory the thread might post a message each time it starts a new file. The alternative is horrible - the thread has to stop doing "real work" so it can process your "request". – John3136 Nov 09 '15 at 05:34
  • @John3136 Okay, that makes sense. Thanks. – JustBlossom Nov 09 '15 at 05:41
  • 2
    You should look at std::thread and not fiddle directly with the Win32 if you can avoid it. – AndersK Nov 09 '15 at 05:41
  • @CyberSpock Is it just safer? – JustBlossom Nov 09 '15 at 05:43
  • @JustBlossom it's just better. In all sorts of ways. Especially argument passing, though. – RamblingMad Nov 09 '15 at 05:44
  • 1
    @JustBlossom it is above all a portable way to work with threads and simpler IMHO. http://en.cppreference.com/w/cpp/thread – AndersK Nov 09 '15 at 05:45
  • There are lots of solutions. `std::atomic<>` is another simple method. Atomic variables have a speed penalty but can be written and read safely across threads. – MSalters Nov 09 '15 at 08:22

2 Answers2

1

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.

CJCombrink
  • 3,738
  • 1
  • 22
  • 38
0

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.

Community
  • 1
  • 1
stas.yaranov
  • 1,797
  • 10
  • 17