-1

I am searching solution to wake-up select call in c++, As per application requirement i cant set timeout because of multiple thread using select system call.

Please see below scenario.

i want to wakeup select system call waiting on other thread. I tried to write data on the thread from main thread but still it is not able to wakeup it.

I want to close thread and socket if there is empty data on this thread.

It is wakes up select call if socket connection is close from other process, but not working with thread.

Does any one have idea regarding this

Atul N
  • 811
  • 1
  • 8
  • 20
  • There is no such thing as empty data in TCP. There are empty datagrams in UDP but it doesn't seem likely that's what you're talking about. Unclear what you're asking. – user207421 Apr 04 '16 at 10:34
  • You can use self pipe trick http://stackoverflow.com/a/384397/2807083 – user2807083 Apr 04 '16 at 10:37
  • 3
    You can also interrupt it with a `signal(3)`. – arrowd Apr 04 '16 at 10:39
  • @EJP I am working on realtime application, whenever there is new thread, it is holding kernal resorves, I want to close all thread when destruction process for the application is going on, so that it will release all resources. – Atul N Apr 04 '16 at 10:55
  • All you have to do is close the socket. I don't see 'realtime' in your question. – user207421 Apr 04 '16 at 11:50
  • @arrowd Thanks for you suggestion, I am trying to write signal(3) handler, but I am not clear how it will help to wakeup select. I am new to signal handling programming – Atul N Apr 05 '16 at 07:20
  • @EJP I have close the socket but still is not able to wake up socket, is anything more i need to add in it. – Atul N Apr 05 '16 at 07:24
  • @arrowd thanks for your suggestion, signal(3) interrupt is useful in case of pselect, it will not wakeup select system call. For pselect we have write signal handle. I may case shutdown() working good :) – Atul N Apr 25 '16 at 07:10

2 Answers2

3

On a recent Linux you can use eventfd, on everything in general - a pipe, usage - register one side of the pipe in selector for readability along with actual socket(s), to wake up a selector - just write one byte to the other end of the pipe. Alternatively (if your libc has it) you can use pselect with a sigmask to catch the ALRM signal and raise that signal whenever you need to wake the selector up. Be very careful with using signals approach in a multithreaded application (as "I would not use"), as if not done right a signal may be delivered to a random thread.

bobah
  • 18,364
  • 2
  • 37
  • 70
-1

Thanks all for valuable suggestion, I am able to resolve the issue with shutdown() call on socket FD using reference answer present on this link, it will pass wakeup signal to select, which is waiting for action. We should close socket only after select call otherwise select will not able to get wake up signal.

Community
  • 1
  • 1
Atul N
  • 811
  • 1
  • 8
  • 20