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 ?
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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With