Archived

This forum has been archived. Please start a new discussion on GitHub.

gracefully terminating blocking calls in Ice::Application and Ice::Thread

Hi all,

Suppose I have a blocking call in an Ice::Thread's run() method -- for example, I'm doing a read() on a datagram socket, waiting for incoming traffic.

Is there a reliable way to terminate the Thread, e.g. by having the read() fail, which can be detected and leads to exiting the run() method? I can do this by accessing the underlying pthread, using pthread cancellation and the PTHREAD_CANCEL_DEFERRED setting; but I was wondering if something similar is possibly strictly within the confines of the Ice API.

Similarly, how can Ice be made to play nice with blocking calls inside an Ice::Application's run() method? E.g. if I do something like this:
int MyApplication::run()
{
    int i;
    std::cin >> i;
    return 0;
}

... I am left with an application that can only be killed using deadly force (kill -9). How can I properly handle this using e.g. the signal handling provided by Ice?

Or should I simply avoid making any blocking calls in the thread/application run() methods?

Best regards, Sidney

Comments

  • mes
    mes California
    Hi Sidney,

    As you've seen, Ice's thread abstraction does not provide support for thread cancellation. This is primarily because Ice itself doesn't need such a feature, but thread cancellation also presents some portability issues and is therefore best avoided.

    There are several ways to handle your first scenario (a thread blocked in a call to read). For example, shutting down the reading side of the socket will cause the call to read to return with an error (Ice uses this solution extensively). You could also send a dummy packet to the socket to wake up read, or you could use a select loop that periodically wakes up to check a status flag.

    As far as Application::run is concerned, have you looked at how the interactive Ice demos (such as demo/Ice/hello/Client.cpp) handle this? They disable Application's built-in signal handling, and instead require the user to issue the proper command to exit the client. However, you could also enable signals and override interruptCallback to deal with the situation yourself, such as by calling exit to terminate immediately.

    Hope that helps,
    Mark
  • Hi Mark,
    mes wrote: »
    For example, shutting down the reading side of the socket will cause the call to read to return with an error

    Ah yes! I have used this before and had forgotten about this. I just implemented it in my trial code and it works like a charm.

    As far as Application::run is concerned, have you looked at how the interactive Ice demos (such as demo/Ice/hello/Client.cpp) handle this?
    No, but I will do that ...
    Hope that helps,
    Very much, thanks!