Main Site Documentation

Data transfer between threads


#1

Hi all,

I got the following situation.
2 threads, one main thread which handles communication with a webservice and another thread, which handles serial communication with another device.
I want to transfer the data from the serial thread to the main thread. What is the best/nicest way to do this?

Regards,
Hullie


#2

How much data are we talking about?


#3

Twice a day approx. 100 lines of data of 120 characters
So about 12 kB @ 9600 Kbit/s

And every minute about 30 characters.


#4

You could try a delegate…look here
http://msdn.microsoft.com/en-us/library/ee436585.aspx
and search the web for c# delegate examples…pretty simple to use.


#5

There are many ways of transfering data. But, delegates did not come to mind. I would have to think a bit how that would work.

I would usually build a queue and use a AutoResetEvent for waking up the processing queue.


#6

I haven’t used them with NETMF yet, but have actually been thinking about several areas lately where I would probably use them…But, I have used them in normal windows applications where I needed to pass processing off to another thread. I have an observatory where I do a lot of astronomical imaging…sending a command to the camera to do a long exposure generally would tie up your application waiting for the process to return unless you throw it into another thread. In that case, I use a delegate to inform me that the process is complete and I can read the data from the camera. Again, not sure what differences there are in NETMF, but I am working on something now using the IO40 that will use the exact same idea…will post on how it works out…we’ll see.


#7

Ah! Are you using an event? I guess you can use a delegate as a callback, but that is old school.


#8

LOL…I suppose it is…but…it works :slight_smile:


#9

Typically this is done with the consumer-producer pattern using something like a thread-safe BoundedBuffer. I did a Blog post here:
http://staceyw1.wordpress.com/2011/02/02/consumer-producer-in-netmf-using-a-boundedbuffer/


#10

Thanks a lot for all the replies. I’ll take a look at it.
The consumer-producer solution is nice I think. In the article there was also a link to the following fezzer articles:

http://www.fezzer.com/project/107/blocking-queue/
http://www.fezzer.com/project/106/monitor2/


#11

I found this useful:
If you are accessing a global object or array from two threads and you don;t want one of the threads to access it when the other thread is accessing it you can use the keyword “lock” to lock the object in a certain piece of code so the other thread waits till the first one is through with the object.


#12

I guess I was thinking he was looking for some sort of callback when the data was ready to be transferred. What Joe suggests is perfectly reasonable solution for simply transferring at any time without an event. Good topic :slight_smile:


#13

“I guess I was thinking he was looking for some sort of callback when the data was ready to be transferred. What Joe suggests is perfectly reasonable solution for simply transferring at any time without an event.”

@ Stephen. It depends on what your doing. Keep in mind that events/callbacks are running in the context of the event source. So that may not be what your after in terms of concurrency. What you described was the consumer/producer pattern, not the event pattern. Either way, you will likely need a lock to protect shared object(s). A lock, as Joe describes, is exactly what we are doing with a shared queue. If you are just sharing an object, it is same concept. Say, for example, it was just a shared string or Int. You need some way to signal other thread when the state changes. How would consumer thread know when data has changed? How would producer know when it was safe to overwrite the value with a new value? You could ping-pong on wait event, but then both threads are blocking again and not doing work and may as well single thead the whole thing. The queue solves that particular issue via buffering which frees both threads to do their own thing and only lock for very short time on queue add/take operations.

P.S. Foklore warning. These patterns developed many moons ago by the long-hairs. :slight_smile:


#14

Coincidently. Channel9 just had good video on this subject and some of the things .Net is adding to address.

BTW - What should we be calling the big .Net? BIGNET -vs- NETMF ?


#15

[quote]Typically this is done with the consumer-producer pattern using something like a thread-safe BoundedBuffer. I did a Blog post here:
http://staceyw1.wordpress.com/2011/02/02/consumer-producer-in-netmf-using-a-boundedbuffer/[/quote]

Nice blog post Will!

Couple of typos:


...
producersWaiting–;
...
consumersWaiting–;
...