I have a function called read() that interacts with some hardware and sometimes it returns the value very quickly and other times it could be very slow. I would like to have a timeout associated with my function read() so that if it takes over maybe a second then the function will timeout. I see people talking about using Boost or multi threading and am not too familiar with either. Can someone help me with this? Thanks.

It's actually just a DLL call so its just

read(&revNum)

With &revNum being set to 0x0 or 0x10. The problem is the case when revNum is being set to 0x0. Sometimes the function takes 5 seconds or more when doing this and I want to make a timeout for the function read()

Problem is I don't have the read() function. The DLL is a compiled binary library. Only thing I have access to is the function prototype.

In that case I don't think you would want to cancel the operation without permission of the read() function, especially since you are passing it a pointer to something that could become invalid if the read operating timed out. If read() tried to copy data to that pointer after your program timed out then read() would most likely crash your entire program because that pointer would no longer be valid.

Without more knowledge about that read() function you are pretty much stuck with current operation.

You can create a thread Sleep for 1 sec & terminate it.

HANDLE threadHnd;
if(threadHnd=CreateThread(<ReadProc>))
{
  Sleep(1000);
  TerminateThread(threadHnd);
}

Yes, he could, but that would most likely crash the program because it would be pulling the rug from under that read() function.

Agreed upon that, but OP in-fact has no choice in the absence of access to the function. Mine was just a crude way to achieve it. It *may* crash it.

Edited 6 Years Ago by nbaztec: n/a

This article has been dead for over six months. Start a new discussion instead.