Dov
Dov

Reputation: 8562

multithreading object-oriented socket code with timeout

We have a nice class that establishes a socket connection and then hits it repeatedly. If it ever gets a timeout, we want to throw an exception.

What I'd like is an independent timeout for each object. However, as noted in other posts there is no portable socket timeout code.

Socket Timeout in C++ Linux

I am currently doing it with a signal, but this is ugly, because there's a single global variable that points to the current object. This means this code could not be multithreaded.

Is there any way of establishing a timer in a per-thread way so that each object could run within its own thread? That seems the cleanest way. I could build a queue so that they all wait in a critical section, but that's extremely ugly and would impact multithreading performance (and I don't want to anyway).

Upvotes: 0

Views: 327

Answers (1)

user405725
user405725

Reputation:

You can do that by using async I/O. For example, using libevent or asio. Just fire up a read operation and a timer on the same dispatching thread (I/O service).

Upvotes: 1

Related Questions