Topic: Is thread cancellation worth the trouble? Look at how QNX does it.
Author: nagle@animats.com (John Nagle)
Date: Thu, 16 Sep 2004 22:18:50 GMT Raw View
QNX has had thread cancellation support for years. It's
not integrated into C++; there's a C-based API. But it's
a widely used real time operating system with thread
cancellation that works.
Under QNX you can cancel a thread at any
cancellation point. All library routines
are documented as being cancellation points or not.
In general, anything that can block is a cancellation point.
The whole POSIX cancellation system is supported.
pthread_cancel is supported, of course. pthread_cleanup_push, etc.
are supported, so you can do destructor-like things in C.
Even asynchronous cancellation is supported, although
the C libraries are not safe with regard to asynchronous
cancellation.
Cancellation works better under QNX than under
Linux/UNIX, because it's a message-passing OS.
All I/O calls are really message passing operations
to other programs. File systems, device drivers,
networking are all user space programs.
Since you can cancel out of a wait for a message
reply, all I/O waits are cancellation points.
So are file opens and network operations. So,
with very few exceptions, if a thread isn't in
a compute loop and you cancel it, it cancels
immediately without any delay.
All this heavy machinery works. But it's
seldom used.
What actually turns out to be more useful is a
QNX-specific call that puts a timeout on the next
"system call" (really a message send in this OS).
You can specify a time limit on
any cancellable system call (which includes all
the ones that block). If the system call takes
too long, it returns with an error code.
This is used frequently. If you don't want to
wait more than 500ms for some I/O operation,
you have that option. In real-time work, this
is common; if something takes too long, you
want to bypass the failed operation and take
corrective action, not sit there stuck.
It's thus worth thinking about whether a general
cancellation mechanism for C++ is worth the complexity.
Some special-purpose mechanisms like this
may be more useful.
John Nagle
Animats
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]