0

In java calls that wait on things, time, IO, semphores, etc will throw an "InterruptedException" when the thread waiting for the operation to complete is "interrupted".

Apparently the "SIGNAL"mechanism used by Pthreads in Linux, std::thread etc is a bit screwed up and difficult to manage.

I basically want to implement a way to abort mutex and semaphore waits, waiting joins etc from an "interrupt" ( kill or whatever ) call to a thread from another thread and catch via exception or return value that fact that this was done inside the thread being "interrupted", without effecting any other running thread.

peterk
  • 5,136
  • 6
  • 33
  • 47
  • `Apparently the "SIGNAL"mechanism used by Pthreads in Linux, std::thread etc is a bit screwed up and difficult to manage.` Threads don't use SIGNALS - they're used to communicate to a process. A std::condition_variable however can be used to communicate between threads. – UKMonkey Jan 02 '18 at 14:41
  • 1
    [Last time I checked](https://stackoverflow.com/questions/2845704/) it was impossible with standard threads. But I remember doing this with Boost threads. –  Jan 02 '18 at 14:44
  • Yes just saw some mention of how signals were used for this purpose but it seemed really nasty having to have a global table for the process. The issue is how to abort the waits. If I write my own semaphores etc using condition variables, and keep a "current waiter" attached to my thread class then I can see it, but all the IO calls, sockets, etc will not abort this way. – peterk Jan 02 '18 at 14:48
  • Well, you should not generalize on mutexes and semaphores. While mutexes are not interrupted by signal (they resume waiting), semaphores are - and it is easy to throw an exception in this case. What do you actually want? – SergeyA Jan 02 '18 at 15:01
  • Pretty simple a way to from one thread, post something to another thread to request a wait in that thread if pending to abort, and then check in that thread whether it was a requested abort ( ie: via interrupt() call I created ) or not. Then perform some action in the "interrupted" thread to continue, clean up and exit, throw an exception or whatever. – peterk Jan 02 '18 at 17:51

1 Answers1

5

Short answer: you can't; the only way you can do this is to perform your wait asynchronously.

Longer answer: Conceptually, if a thread could force exceptions to be thrown as an interrupt in other threads, they could do this at any time; not just during your wait or join or io.

It is not always safe to throw an exception - for example in a destructor - since if the stack is already being unwound because of an exception, trying to throw another exception will result in trying to handle 2 exceptions at once - which causes problems. This is true in Java so shouldn't be an unfamiliar concept.

Java and C# are not C++; their thread object does not represent by requirement a native thread; as such everything they do is likely already asynchronous under the hood to prevent thread starvation in the cases where the number of threads allowed is less than the number of thread objects.

This difference is very notable in the thread::abort style methods; which are fine in C# and Java, but very dangerous in C++ since it terminates the thread immediately in C++ whatever it is doing, including being in the middle of new() which may be locking a mutex - causing all further calls to new() in your application to deadlock.

UKMonkey
  • 6,941
  • 3
  • 21
  • 30
  • I guess right - the issue is usually a thread is waiting on an IO operation on a pipe, socket, timer, whatever and there is no OS integrated way to force some of these operations that wait on a semaphore or mutex internally to abort and indicate their aborted status in a number of cases without having to abort the whole process, and this often bypasses any cleanup code one might have in the thread. – peterk Mar 28 '18 at 21:33