Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending messages between threads in C#

How can I send and receive messages between threads?

like image 889
Ankata Avatar asked Nov 21 '10 10:11

Ankata


People also ask

How do I send messages between two threads?

How can I send messages between threads? class A extends Thread { List<Object> objs = something; //Init it void run() { while(true) { //Body which works on objects. //After receiving an external message, "A" should perform some action, for example, empty objects. } } }

Can threads use message passing?

Threads in the same Java Virtual Machine (JVM) can communicate and synchronize by passing messages through user-defined channels that are implemented as shared objects.

How do you communicate between threads in Linux?

The easiest way for multiple threads to communicate is through memory. If two threads can access the same memory location, the cost of that access is little more than the memory latency of the system.


2 Answers

One solution would be share a concurrent queue, for example (albeit its name) ConcurrentQueue. This will allow you to enqueue an object from one thread and have the other thread (or others threads) dequeue from the queue. As it is a generic solution, you may pass strongly typed items, anything from string to Action will do, or your own custom message class of course.

Threre is just one limitation with this approach, the class ConcurrentQueue is only available from .NET 4.0 onwards. If you need this for a previous version of .NET you need to look for a third party libary. For example you can take the source for ConcurrentQueue from mono.

The general approach over which those queues work is by having a linked list and they resource to optimistic concurrency control using spinning for synchronization. As far as I know, this is the state of art for concurrent queues of variable size. Now, if you know the message load beforehand you can try a fixed size approach or a solution that favors enqueue and dequeue over growing (that would be an array based queue).


Full disclouser (according to faq): I'm the author of one of those third party libraries... my libraries (nuget available), it includes a backport ConcurrentQueue for old versions of .NET, based on a custom implementation. You can find the underlaying structure under Theraot.Collections.ThreadSafe.SafeQueue, it is a linked list of arrays (which are kept in an object pool), by doing it this way, we do not need to copy the arrays to grow (because we just add another node to the list), and we do not need to rely on synchronization mechanisms as often (because adding or removing an item does not modify the list often).

Note: this question used to link to HashBucket, which is hosted on another repository, and was my old solution for the problem. That project is discontinued, please use the version I mention above.

like image 119
Theraot Avatar answered Nov 15 '22 08:11

Theraot


This is an old question, but still a relevant topic...

A producer/consumer approach may be used as possible solution for a problem like this. .NET Core, from version 3.0, has a namespace with tools to deal with that in a simple way.

Take a look at System.Threading.Channels:

https://docs.microsoft.com/en-us/dotnet/api/system.threading.channels https://devblogs.microsoft.com/dotnet/an-introduction-to-system-threading-channels/

like image 30
Victor Magno Avatar answered Nov 15 '22 09:11

Victor Magno