Callback between Threads

Callback between Threads

Post by davidpryce » Mon, 27 Apr 2009 19:10:44

Hi Group,

I am attempting to write a program that has two threads using PThreads
in C++ on Mac OSX.

The main thread is a GUI. The second thread reads from a USB to
serial device with a blocked read.

I have been looking for an C++ OO and thread safe technique for the
USB thread to inform the GUI thread about the receipt of data or a
change in the RS232 signals.

Basically in my main function I start a thread that read from the USB
device. At the moment I an just writing the data read from the USB to
stdout to confirm that the USB code works okay.

From what I have read on the net is the best approach is to use a

Does anyone have a reference to simple an C++ OO example that might
explain how I can do this?

I am rather new to multi threaded applications and I would like to
understand the best technique to use.



Callback between Threads

Post by David Schw » Tue, 28 Apr 2009 13:15:34

Why? Why not just have the thread that already knows that data has
been received do whatever needs to be done when data is received?
What's the logic of handing the information off to another thread when
one perfectly good thread already has the information.

I'm not sure what you mean by a callback. If you must hand it off to
another thread, probably the best approach is to queue the object to
the list of events the GUI thread already handles. If the GUI thread
is event driven, just add a 'USB data received' event to its incoming
event queue.