c - How to interrupt a fread call? -


i have following situation:

there thread reads device fread call. call blocking long there no data send device. when stop thread remains hanging inside thread.

now found following inside man page of fread:

errors

on systems conform single unix specification, fread() function sets errno listed following conditions:

[eintr] read operation terminated due receipt of signal, , no data transferred.

that mean there way interrupt call different thread. have no idea how. can tell me how send signal interrupt fread call? , signal need send?


update 08-10-10 09:25

i still haven't got work. tryed kill() , pthread_kill() different signals. nothing seems interrupt fread() call. thing got working killing entire application, that's not want.

1. signals:

using signals, many others pointed out, work. however, many others pointed out, approach has disadvantages.

2. select():

using select() (or other multiplexing function), can block waiting data arrive more 1 file descriptor, , specify timeout.

use timeout advantage. whenever select() returns, check global variable see if must terminate. if want immediate reaction, keep reading.

3. select() , pipes:

multiple fds means can wait data arriving through device mentioned and, say, pipe.

before create thread, create pipe, , have thread block on select() monitoring both device , pipe. whenever want unblock select whether device has new data or not, send byte down pipe.

if select() tells unblocked due data arriving through pipe, can clean , terminate. note method more flexible signaling method, since can, besides using pipe wake-up method, use pass useful information or commands.

4. select(), pipes , signals:

if using multiple processes , don't want to/can't pass around pipe, can combine both solutions. create pipe , install signal handler for, say, sigusr1. in signal handler, send byte down pipe.

whenever process sends sigusr1, handler called , unblock select(). examining fdsets, know no other reason own program signaling itself.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -