0

I'm sorry if this question is too easy to solve. I would like to implement the following scenarios in C++. There exists a collection of functions to be evaluated like f_1, f_2, etc. While evaluating f_i, the program is sending and receiving something to or from another host. When f_i finishes, there is some return value. So the program should immediately move to socket part to send the value or receive something from another machine. But at the same time, computation of f_j which is not evaluated now should start.

I know multi-threading may solve this problem. But, how a one thread knows if a computation in some specific thread finishes?

If the socket is replaced by File I/O, I think we can do same thing.

It would be really appreciate if you suggest me a way to solve this or some reference to do that.

user9414424
  • 498
  • 3
  • 15

1 Answers1

0

You should probably have at least one I/O thread with an event loop to handle your sockets.

This I/O thread can dispatch computations to a thread pool. Once the computation finishes you should let the I/O thread know that it should send the computation result. There a few methods to do that, one simple method is for the compute thread to allocate the computation result on the heap and write a pointer to it into a pipe. The I/O thread event loop notices that there is data in the read end of the pipe available, reads the pointer to the result and starts sending it in non-blocking fashion.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271