public static bool Run() { int x = 0; m_threadStart = Child; IntPtr[] h = new IntPtr[2]; IntPtr[] e = new IntPtr[2]; e[0] = NativeMethods.CreateEvent(IntPtr.Zero, false, false, null); e[1] = NativeMethods.CreateEvent(IntPtr.Zero, false, false, null); h[0] = CreateNativeThread(m_threadStart, e[0]); h[1] = CreateNativeThread(m_threadStart, e[1]); lock (lock_me) { System.Console.WriteLine("Parent"); x++; } unsafe { fixed(IntPtr *handlePtr = e) { NativeMethods.WaitForMultipleObjects(2, handlePtr, true, Timeout.Infinite); } } NativeMethods.CloseHandle(e[0]); NativeMethods.CloseHandle(e[1]); NativeMethods.CloseHandle(h[0]); NativeMethods.CloseHandle(h[1]); return(true); }
public static IntPtr CreateNativeThread(Win32ThreadStartDelegate p) { IntPtr n_nativeFunction = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(p); //ch = p; IntPtr handle = NativeMethods.CreateThread( IntPtr.Zero, IntPtr.Zero, n_nativeFunction, IntPtr.Zero, 0, IntPtr.Zero); return(handle); }
public static bool Run() { int x = 0; m_threadStart = Child; IntPtr h = CreateNativeThread(m_threadStart); lock (lock_me) { System.Console.WriteLine("Parent"); x++; } NativeMethods.CloseHandle(h); return(true); }
public static global::System.IntPtr CreateThread( global::System.IntPtr lpThreadAttributes, global::System.IntPtr dwStackSize, global::System.IntPtr lpStartAddress, global::System.IntPtr lpParameter, int dwCreationFlags, global::System.IntPtr lpThreadId ) { return(Helper.SimpleWrap <global::System.IntPtr>( delegate(ClrSyncManager manager) { Original.Semaphore semaphore = new Original.Semaphore(0, 1); int childThread = manager.TaskFork(); Win32ThreadStartDelegate wrapper = delegate(global::System.IntPtr argPtr) { try { manager.ThreadBegin(semaphore); int returnValue = 0; Exception exception = null; Microsoft.ManagedChess.MChessChess.LeaveChess(); try { returnValue = CallThreadFunction(lpStartAddress, lpParameter); } catch (Exception e) // catch recoverable exception in monitored code { exception = e; } Microsoft.ManagedChess.MChessChess.EnterChess(); if (manager.BreakDeadlockMode) { Microsoft.ManagedChess.MChessChess.WakeNextDeadlockedThread(false, true); } else if (exception == null) { manager.ThreadEnd(childThread); } else { manager.Shutdown(exception); } return returnValue; } catch (Exception e) // catch fatal exception in our code { manager.Shutdown(e); return -1; } }; //make sure wrapper does not get GCed manager.PinObject(wrapper); global::System.IntPtr wrapperPointer = Marshal.GetFunctionPointerForDelegate(wrapper); global::System.IntPtr returnVal = OrigNativeMethods.CreateThread(lpThreadAttributes, dwStackSize, wrapperPointer, global::System.IntPtr.Zero, dwCreationFlags, lpThreadId); if ((dwCreationFlags & OrigNativeMethods.CREATE_SUSPENDED) == 0) { manager.TaskResume(childThread); } manager.RegisterTaskSemaphore(childThread, semaphore, true); global::System.IntPtr childHandleCp; bool dupret = OrigNativeMethods.DuplicateHandle( OrigNativeMethods.GetCurrentProcess(), returnVal, OrigNativeMethods.GetCurrentProcess(), out childHandleCp, 0, false, OrigNativeMethods.DUPLICATE_SAME_ACCESS); global::System.Diagnostics.Debug.Assert(dupret); manager.AddIJoinable(childThread, new NativeThreadJoinable(childHandleCp)); manager.AddNativeHandleForSyncVar(returnVal, childThread); return returnVal; }, delegate() { // default to direct call return OrigNativeMethods.CreateThread(lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, lpThreadId); } )); }