PDA

View Full Version : How to handle data provided by thread created from external library?



bamboosso
28th April 2015, 16:55
Hey,

I have a library to communicate with sensors and different hardware. This library creates internal thread and allows me to bind receiver function. its non qt lib.

For now I know that new data is generated every 100ms, so I have created QTimer that invokes every 100 ms and "eat" this data. QMutex is protecting both threads. It works, but I don't think that this is correct/best solution.

How can I push data (std::vector<int>) to SLOT of my main window?

Thanks for suggestions.

anda_skoa
28th April 2015, 17:14
You say you can "bind a receiver function".
I assume that means that the library allows you to specify a function that is invoked whenever there is new data.

The easiest way is probably to emit a signal transporting the data as its argument right from that function.

Cheers,
_

bamboosso
29th April 2015, 08:19
Thanks!

but...

1. I would like to prevent creating a copy of data (lot of them).
I was thinking about some kind of swap with vector created in thread with internal vector of my mainwindow class.
Pass reference to slot?? is it possible??
Allocate data and pass to slot using some kind of shared_ptr?

2. Is it safe to emit signals not from QThread?

anda_skoa
29th April 2015, 09:11
1. I would like to prevent creating a copy of data (lot of them).

You could put the data into some kind of mutex protected storage and then only signal new availability to the main thread.



Pass reference to slot?? is it possible?

Not across threads.



Allocate data and pass to slot using some kind of shared_ptr?

That might work.



2. Is it safe to emit signals not from QThread?

Hmm, good question.
You could ensure that a Qt::QueuedConnection is used by passing that to the connect() statement, or invoking the slot via QMetaObject::invokeMethod() and passing it there.

Cheers,
_

bamboosso
29th April 2015, 13:54
Qt::QueuedConnection <-- thank you for that. Saved me a lot of time. I'm new in Qt...

Cheers