Efficiently pass data from thread to main

Using boost, I start a thread from the main process as such. MyWorkThread newThread; newThread = boost::thread(newThread); And in the worker thread class (MyWorkThread), I have a read function which reads from a USB device as much data as it can deliver. This function can be called several hundred times a second. MyThread::read() } //... while () { MyData data; if (hasMore(dev, &data)) // lots of data here // need to get it back to the main thread } } } I'm trying to figure out how to efficiently get this data back to the main process to be placed in a buffer since it can be called so often. - Would making a callback to the main process be the way to go? If so, any pointers how using boost? - Or should I just have a global data buffer and store it directly?

Many ways to do it; simply put, the thread need to be able to put the data into a buffer that the main thread can read from, in a thread safe manner. My preferred way of doing this is using a producer-consumer pattern; basically a queue with many producers and one consumer. You may want to look at Boost.LockFree; especially the example http://www.boost.org/doc/libs/1_57_0/doc/html/lockfree/examples.html The global data buffer is nice too; make sure you use a thread safe buffer ( or data structure ). http://www.boost.org/doc/libs/1_53_0/libs/circular_buffer/doc/circular_buffe... (make sure you read #threadsafety <http://www.boost.org/doc/libs/1_53_0/libs/circular_buffer/doc/circular_buffer.html#threadsafety> ) Good luck On 5 May 2015 at 00:09, Lane <software.research.development@gmail.com> wrote:
Using boost, I start a thread from the main process as such.
MyWorkThread newThread; newThread = boost::thread(newThread);
And in the worker thread class (MyWorkThread), I have a read function which reads from a USB device as much data as it can deliver. This function can be called several hundred times a second.
MyThread::read() } //... while () { MyData data;
if (hasMore(dev, &data)) // lots of data here // need to get it back to the main thread } } }
I'm trying to figure out how to efficiently get this data back to the main process to be placed in a buffer since it can be called so often.
- Would making a callback to the main process be the way to go? If so, any pointers how using boost? - Or should I just have a global data buffer and store it directly?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
DK
-
Lane