Your problem is a classic example. The consumer problem of the manufacturer .
There are several possible ways to implement this depending on the context: your post is tagged with both pthreads and linux-device-drivers . Is this space kernel-space, user space, or kernel space->?
Kernel space
The solution will probably use a ring buffer (if you expect several messages between threads to be in flight at once) and a semaphore.
Chapter 5 Linux Device Drivers 3rd Edition is a good place to start.
User space
If both threads are in user space, the producer-consumer pattern in the same process is usually implemented with the pthread variable. A pthread example of how to do this is here
Core Space โ User Space
The general approach used by Linux is to thread the user space thread_2 to lock the registration system object passed through the kernel space thread_1 . Typically, the registration system object in question is located in /dev or /sys . LDD3 has examples of both approaches.
source share