Home | Projects | Notes > Multi-Threading (POSIX Threads) > Inter-Thread Communication

Inter-Thread Communication

 

Introduction to Inter-Thread Communication

A big software system may be a multi-threaded software where multiple modules are running as independent threads of the same process and the threads may need to exchange data with one another. In this case, we need to set up communication between threads (i.e., exchange of data).

Inter-Process Communication (IPC), the techniques used to set up the data exchange between processes, can also be used for the communication between threads. The followings are the tools used to set up the IPC:

However, for Inter-Thread Communication, using callbacks (or function pointers) is preferred to using IPC techniques. This is because callbacks are:

 

Transfer of Data vs. Transfer of Computation

Let's say there are two entities (i.e., processes or threads) that may or may not be running on the same machine:

There are two possible scenarios where Entity 1 can compute a * b:

In both cases, the resultant value will be saved only on Entity 2's machine.

 

Example - Transfer of Computation

The following is a simple example that demonstrates the transfer of computation.

Step 1: Thread 2 registers the multiply() function to the Thread 1's function pointer fn_ptr.

Step 2: Thread 1 generates the data and performs the multiplication using the function registered to fn_ptr.

Above example shows important properties of this model:

 

Publisher Subscriber Model (a.k.a. Notification Chain)

Transfer of Computation (TOC) leads to a famous architectural communication mode called the Publisher Subscriber Model (a.k.a. Notification Chain):

This model is widely used to set up the Inter-Thread Communication.

 

 

References

Sagar, A. (2022). Part A - Multithreading & Thread Synchronization - Pthreads [Video file]. Retrieved from https://www.udemy.com/course/multithreading_parta/