communication between threads and/or processes

I would like to run two C++ process / threads independently with one as "sender" and the other "receiver".

For instance, "sender" will read keyboard instruction live and convert into commands, which will be sent to "receiver" which will do its thing (eg. log into file).

How do I do that if they are different threads on the same process, and if they are running on different process? Thanks in advance. Note that I intend to send a lot of messages between the two over a short period of time as a burst. How would I know what the limit is?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void main() {
  // sender
  string input("");
  while (true) {
    cout << "Send your instruction (A)sk, (S)ing, (Q)uit : ";
    getline (cin, input);
    if (input == "A") {
      //send "ask" message to receiver, how?
    } else if (input == "S") {
      //send "sing" message to receiver, how?
    } else if (input == "Q") {
      break;
    }
  }
}
=================================================================
// running in another thread or process
// somehow it receives the message from sender and call this function
void receiver(string input) {
  // receiver
  if (input=="A") {
    cout << "You ask a lot of questions!" << endl;
  } else if (input = "S") {
    cout << "Sing sing sing sing sing sing sing...." << endl;
  } else {
    cerr << "Unknown input " << input << endl;
  }
}
  
You need to do a search on "interprocess communication", often called IPC.

The techniques available vary between Operating Systems.
I've searched but can't find any example code. I'm running C++ on a Window's machine. Win8 and later Win2003 to be precise.

Are there any hands on simple example code available? I had been searching but can't find any
Threads share memory. They can see same memory locations.

receiver (pseudo):
1
2
3
4
5
6
while ( true ) {
  string command;
  if ( ! input.empty() ) { command = input; input.clear(); } // *1*
  // use command
  // wait
}


sender (pseudo):
1
2
3
4
while ( true ) {
  string task = // get input
  input = task; // *2*
}

Now, we cannot allow that *2* occurs while *1* is in the process of evaluating. Both sides have to "lock" access to input with "mutex". Actually, the *2* should wait until the input is empty, so maybe have a queue<string> as input, that the *2* pushes to and *1* pops from. The mutex locks are still required.

Inter-process version: the receiver listens a "socket" (opens a socket and occasionally attempts to read from it). The sender writes to the "socket". (The socket could be a network port too, i.e. processes could be in different machines.)


Sorry, no code examples.
I'm running C++ on a Window's machine. Win8 and later Win2003 to be precise.

http://msdn.microsoft.com/en-gb/library/windows/desktop/aa365574%28v=vs.85%29.aspx
The link referes to: Clipboard, COM, Data Copy, DDE, File Mapping, Mailslots, Pipes, RPC, Windows Sockets.

I would use File Mapping. Both ends share a block of memory and can read/write at will. You will need to synchronise access.

COM? I just wouldn't bother.

DDE? I thought this was deprecated 20 years ago. Just forget it. It's a precursor to COM and needs a server with very careful use.

Clipboard. Novel, and gives the the ability to see the messages by pasting into notepad--but really ...

Data Copy uses the clipboard in a particular way.

Mailslots and Pipes are Windows abstractions of datagram and streamed network communications. If you already know UDP/TCP and don't already know them, don't bother learning them.

RPC. No one uses RPC on Windows. It's been replaced by DCOM.

Windows Sockets. Again, more than you need, but if you ever decide to move your client and server onto different computers, it's exactly what you need.
File Mapping would be ideal in this situation because then the "limit" on the amount of data you are able to communicate will be arbitrarily large. Just a note, do NOT rely on exceptions like MSDN suggests for this task. That is just sloppy coding. Use a Mutex to synchronize the reading and writing like a normal person would.
Have anyone uses Message Queueing?

http://msdn.microsoft.com/en-us/library/ms711472%28v=vs.85%29.aspx

Was it any good in terms of latency if I want guaranteed but fast messaging among different machines on the local network? Or the same box?
I worked on a project that used it. It was slow, but so were the machines. I don't know what it would look like on a modern machine.

If I were to use queuing from C/C++, I'd look at ZeroMQ.

The web services folk where I work use RabbitMQ. They seem to like it, but they like PHP, so they're judgement could be off.

So much for the free stuff.
Topic archived. No new replies allowed.