0

I have a thread that waits on a blocking call (via select) and want it to communicate with the parent thread at the same time. Since, it can be involved in a blocking call when parent sends it a message, I cannot use WaitForMultipleObjects. I was wondering if I can use a socket between child and parent thread, but all literature suggests that sockets are best used for inter-process and not inter-thread communication. At the same time I dont find a reason of how they may not fit my use case. Is there anything I may be missing or is there another solution for such a use case. (Looking for a c++ based solution)

rajat
  • 864
  • 1
  • 9
  • 23
  • Have you tried using [std::condition_variable](https://en.cppreference.com/w/cpp/thread/condition_variable) for waiting, so that you could wake the thread up from parent thread when needed? – Killzone Kid Jun 18 '18 at 08:11
  • A thread waiting on select won't be sleeping as far as parent is concerned, so I doubt if it would work. Thanks for the suggestion though! – rajat Jun 18 '18 at 08:24

3 Answers3

1

Set a timeout for select and run it in a loop so you can periodically communicate with the parent thread via memory.

Or run the select in a separate third thread and wait for it in the second thread using a std::condition_variable with a timeout in a loop or other means, while also being able to communicate with the parent thread.

krisz
  • 2,686
  • 2
  • 11
  • 18
1

wondering if I can use a socket between child and parent thread

Yes. You can.

but all literature suggests that sockets are best used for inter-process

The main reason (maybe the only reason) for choosing to use multiple threads within one process instead of using multiple processes to implement an application, is that the threads can communicate with one another through shared memory. This can simplify the design of the application because data do not have to be marshalled, and sent through pipes, and un-marshalled at the other end.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • P.S., I don't know so much about WinAPI threads, but in many threading systems, there is no parent/child relationship between threads. It may help _you_ to think of them as parent and child, but the threading library itself probably does not care which one created which other one. – Solomon Slow Jun 18 '18 at 12:38
1

I have a thread that waits on a blocking call (via select) and want it to communicate with the parent thread at the same time. Since, it can be involved in a blocking call when parent sends it a message, I cannot use WaitForMultipleObjects.

You cannot use WaitForMultipleObjects() to wait on a SOCKET handle.

However, if you use WSAEventSelect() instead of select() to wait on a socket operation, you can then use WaitForMultipleObjects() or WSAWaitForMultipleEvents() to wait on the socket event along with other Win32 objects at the same time, like event objects, pipes, etc.

Or, if you can use PostThreadMessage() to post messages between threads, you can use MsgWaitForMultipleObjects() instead.

Otherwise, you will just have to call select() with a short timeout, and then check your inter-thread comms as needed in between calls to select().

I was wondering if I can use a socket between child and parent thread

Technically yes, but it is not very beneficial to do so. There are more efficient ways to communicate between threads.

all literature suggests that sockets are best used for inter-process and not inter-thread communication.

That is correct.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770