I'm trying to send a message from one thread to another. Each thread knows the thread ID of the other. How can I send a message between them?
I've already seen some proposed solutions (message queue, anonymous pipe, etc.) but honestly I didn't get them to work. Obviously I didn't understand the previous descriptions enough, hence this topic.
So to sum up, just the shortest possible way to send, let's say a message "Hello!" from thread to another thread, make the 2nd thread show it on stderr, and than send back to 1st thread message 'Hello back!'.
It's probably very easy and I didn't do a good job of researching, but I've been stuck for some time now, and can't find decent way to do this.
An example, it's pretty simple — first make a pipe with
pipe()
. It creates two file descriptor — one for reading, and the second for writing. Here we calling it two times to have both read and write sides. Then we calling fork (that makes a second thread), and write/read messages through the pipes we created.