Interrupting your own streams

I am currently looking into how Thread.Interrupt plays alongside P / Invoke or native calls. I read on MSDN that it is not possible to cancel (Thread.Abort) a thread that is in a normal call (other use cases may apply as well). But I haven't found a link that claims the same for native threads that are in a WaitSleepJoin state.

This question is not about whether to trigger an interrupt or an interrupt, but about where I can find the official doc. G-ing did not provide useful output for this.

My test case:

#ifdef NATIVEFUNCTIONS_EXPORTS
#define NATIVEFUNCTIONS_API __declspec(dllexport)
#else
#define NATIVEFUNCTIONS_API __declspec(dllimport)
#endif

#include <iostream>

extern "C"
{
  NATIVEFUNCTIONS_API void EndlessWait(char const* mutexName)
  {
    std::cout << "entering the endless wait." << std::endl;

    HANDLE mutex = CreateMutex(NULL, FALSE, mutexName);
    WaitForSingleObject(mutex, INFINITE);

    std::cout << "leaving the endless wait." << std::endl;
  }

};

      

Native C ++ is a DLL that exports a function that waits for a mutex indefinitely.

Now the C # .NET instance that is trying to cancel the wait:

using System;
using System.Threading;
using System.Runtime.InteropServices;

namespace InterruptingNativeWaitingThread
{
  class Program
  {
    [DllImport("NativeFunctions.dll", CharSet=CharSet.Ansi)]
    static extern void EndlessWait(string str);

    static void Main(string[] args)
    {
      string mutexName = "interprocess_mutex";
      Mutex m = new Mutex(false, mutexName);
      m.WaitOne();
      Thread t = new Thread(() => { EndlessWait(mutexName); });
      t.Start();
      Thread.Sleep(1000);

      t.Abort();
      if(!t.Join(5000))
        Console.WriteLine("Unable to terminate native thread.");

      t.Interrupt();
      if(!t.Join(5000))
        Console.WriteLine("Unable to interrupt the native wait.");

      Console.WriteLine("Release the mutex.");
      m.ReleaseMutex();
      t.Join();
    }
  }
}

      

Running this application produces the following output:

entering the endless wait.
Unable to terminate native thread.
Unable to interrupt the native wait.
Release the mutex.
leaving the endless wait.

      

Abort does not work in this context as expected, but msdn does not say a word about aborting. I expect this to work on the one hand: because the pending managed thread also calls its own WaitForSingleObject or WaitForMultipleObjects; on the other hand, there is a possibility that the native thread that will be interrupted does not support pending exceptions anything less?

Any document is greatly appreciated!

Thanks a lot,
Hovhannes

PS I also found on MSDN that abort waits for the thread to be interrupted to return from unmanaged code and first interrupt the call if the thread is in WaitSleepJoin rather than canceling it. But this does not mean that an interrupt cannot interrupt its own WaitSleepJoin.

+2


source to share


1 answer


I doubt the thread is in the WaitSleepJoin state; The interrupt is only documented for interrupting threads in this state. Look at the ThreadState property of a thread to see what state it is in.



+2


source







All Articles