Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message passing between threads in C

I am trying to get Thread A to communicate with Thread B. I should be using message passing between threads to do this but I am trying to find some sample source code which explains message passing.

Does anyone have any good link to some sample source code (in C) which explains message passing ?

like image 426
Ashish Agarwal Avatar asked Nov 24 '11 07:11

Ashish Agarwal


People also ask

How do you pass messages between threads?

Use a synchronized queue for message passing between threads. The queue serves the same function as the buffered network communication channel in client/server message passing.

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 two threads communicate with each other in C?

One way is to use message passing between threads via asynchronous queues. This way you can avoid using shared data between threads and only the queues need to be thread-safe. Asynchronous queues can be implemented using different synchronisation primitives: Pipes or sockets.

What is message passing with example?

In computer science, message passing is a technique for invoking behavior (i.e., running a program) on a computer. The invoking program sends a message to a process (which may be an actor or object) and relies on that process and its supporting infrastructure to then select and run some appropriate code.


2 Answers

While not having a link, there are many ways to implement this.

  • First is to use sockets. This is not actually a method I would recommend, as it can be quite a lot of work to get it to work right.

  • The second is related to the first method, and is to use something called anonymous pipes.

  • A third way, and the one I usually use, is "inspired" by how message passing worked on the old Amiga operating system: Simply use a queue. Since memory is shared between threads it's easy to just pass pointers around. Use one queue per thread. Just remember to protect the queues, with something like a mutex.

  • The platform you are using will probably have other ways of communication. Do a Google search for (for example) linux ipc.

like image 107
Some programmer dude Avatar answered Sep 19 '22 01:09

Some programmer dude


ONe very easy way that's fairly fast on Linux at least is to use either TCP or UDP sockets for message passing between threads. The Linux kernel is pretty smart and if I remember correctly it will bypass the network stack which makes it pretty fast. Then you don't have to worry about locking and various other issues which are basically handled by the kernel. Should be good enough for a homework assignment.

Uri's TCP/IP Resources List FAQs, tutorials, guides, web pages & sites, and books about TCP/IP

like image 41
Robert S. Barnes Avatar answered Sep 22 '22 01:09

Robert S. Barnes