Determining interest for a thread library extension to handle blocking situations

Currently the threading library does not support for handling situations where a thread might be blocked on IO, in a platform independant way. While it is possible to write (low level) IO such as networking code that has the ability to be unblockable, the lack of an uniform interface seems unfortunate. Say thread B currently is blocking on a (third party) IO library. Now thread A needs to abandon thread B. How should this be done? Possible options are: 1) A schedules a cancel request to B 2) A calls a certain function of the IO library that, given a thread ID is able to unblock the current operation. Ad 1) this is currently not supported by boost thread, and given the rather controversial discussion about correct semantics in the past, it is unlikely to happen soon. Ad 2) This puts a lot of burden onto the user of the library. Interfaces also are likely to be different for various IO libs. Desirable would be a (free) function, say "alert(thread-id)" that is given the thread-id of B. When called by thread A, whatever is necessary to wake up the thread B will be done. The point is that my proposed library does not do the real actions, but only supply a framework that will allow the IO library to register a function to be called when it is time to break out of IO. To find out whether this is feasible I wrote a prototype implementation and an example to demonstrate the use of the interface. (Altough currently only available for MSVC.) My approach is two step. The first of which even might be interesting for a library on its own. 1) A "thdmbrptr" thread member pointer lib The intent of this library is to allow two threads to establish a shared memory space, without requiring the user code to pass any information. This basically does work like a thread local storage from inside the thread: boost::thread_member_ptr<myclass> pmyclass; All functions known from thread_specific_ptr are available, and so is semantics from inside the thread. Besides this another thread can get access to the data by: pmyclass[th]->foo(); where "th" is a thread id (or boost::thread*) and "foo()" is a function of "myclass". The lifetime of the myclass instance is managed by a shared_ptr. One reference is held by the thread (by means of a tss), a second is held by the boost::thread object that created the thread, and additional references might be held by other threads that obtained it by "*pmyclass[th]". 2) An "alert" lib The alert lib has two functions and a class object. alert(boost::thread* th) . . . to alert a thread bool alerted() . . . to find out (and reset) the alerted state of a thread class alerter . . . a class that will be instantiated on the stack. The constructor registers a (IO - ) lib defined function, The destructor unregisters this function. Some additional notes: I have been able to write a prototype implementation that 1) does not need any change to the current boost::thread 2) does not contain platform dependant code since it is built entirely on top of other boost libs. Altough it would by surely desirable to modify boost::thread to give a cleaner interface and improve performance. (Given the alert-ability of threads, a cancellation might later be built on top of it.) If it turns out, that there is interest in my proposal I will upload my files into the "vault". Thank you for your attention, Roland

Roland Schwarz wrote:
Currently the threading library does not support for handling situations where a thread might be blocked on IO, in a platform independant way.
While it is possible to write (low level) IO such as networking code that has the ability to be unblockable, the lack of an uniform interface seems unfortunate.
Say thread B currently is blocking on a (third party) IO library. Now thread A needs to abandon thread B. How should this be done? Possible options are: 1) A schedules a cancel request to B 2) A calls a certain function of the IO library that, given a thread ID is able to unblock the current operation.
I think there is a genuine need for a portable interface that provides a way to implement "cancellation semantics." I think this is a pattern commonly needed in threaded code. It's my impression that POSIX cancellation in C++ is still in a really sorry state, and its unclear where this is going. I don't know if there are any implementations that have implemented cancellation for C++ to a point that its really safe or usable. (Does anyone know more about what is happening with this? Is anyone involved in the POSIX or C++ standards working on this right now?) Also, Windows doesn't appear to have any native support at all for gracefully shutting down a thread: TerminateThread() doesn't attempt to do any sort of unwinding, and appears to have a tendancy to leak resources.
My approach is two step. The first of which even might be interesting for a library on its own.
What do you think about merging adding this to the Boost.Thread library rather than making it separate? thread_member_ptr in particular appears to nicely compliment the thread_specific_ptr. It also appears that implementation would be simplified if thread_member_ptr were part of Boost.Thread.
1) A "thdmbrptr" thread member pointer lib
I really like this class, but I do think it needs a better name.
2) An "alert" lib
As mentioned above, I think something of this nature is needed. However, I am unsure of the interface you're proposing. I would like to see more experience using the alert class in real-world code. I need to think about this some more.
If it turns out, that there is interest in my proposal I will upload my files into the "vault".
I definitely think you should upload the files to the vault. Aaron W. LaFramboise

Aaron W. LaFramboise wrote:
I definitely think you should upload the files to the vault.
I have uploaded the file, and it can be accsessed from the below link: http://boost-sandbox.sourceforge.net/vault/index.php?action=downloadfile&directory=&filename=threadalert.zip Roland
participants (2)
-
Aaron W. LaFramboise
-
Roland Schwarz