This is the mail archive of the pthreads-win32@sources.redhat.com mailing list for the pthreas-win32 project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Implementation of pthread_kill() ...


Did someone try to implement this.

Before doing it, I'm trying this little program, but it hangs :

Any idea ?

thank for reply

Sylvain

The program :

#include <windows.h>
#include <process.h>
#include <stdio.h>

///////////////////////////////////////////////////////////////////////////////
// Macros used to abstract the instruction pointer register for the various
// CPU platforms.


#if defined(_X86_)
#define PROGCTR(Context)  (Context.Eip)
#endif

#if defined(_MIPS_)
#define PROGCTR(Context)  (Context.Fir)
#endif

#if defined(_ALPHA_)
#define PROGCTR(Context)  (Context.Fir)
#endif

#if defined(_PPC_)
#define PROGCTR(Context)  (Context.Iar)
#endif

#if !defined(PROGCTR)
#error Module contains CPU-specific code; modify and recompile.
#endif


// The thread entry point
unsigned int WINAPI threadfunc( void * p)
{
     int i = 0;
     while(1)
     {
	printf( "Coucou from the thread (iteration %d)\n", i++ );
     }
     return 0;
}

// The signal function
static CONTEXT s_LastContext;
static void WINAPI SignalFunc(void)
{
     // Calling the signal
     // ...

     // returning to last adresse
     SetThreadContext( GetCurrentThread(), &s_LastContext);
}


void main()
{
     HANDLE hThread;
     DWORD IDThread;
     int param = 0;

     // Crete the thread
     hThread = (HANDLE)_beginthreadex( NULL, 0, threadfunc, (void *)param, 
0, (unsigned int *)&IDThread );

     // Waiting the thread is running
     Sleep(2000);

     //
     CONTEXT context;

     // Stop the worker thread
     SuspendThread(hThread);

     if (WaitForSingleObject(hThread, 0) == WAIT_TIMEOUT)
     {
	 // The worker has not yet terminated

	 // Get the worker thread's current CPU registers
	 context.ContextFlags = CONTEXT_CONTROL;
	 GetThreadContext(hThread, &context);
	 s_LastContext = context;
	 //memcpy( (void *)&s_LastContext, (void *)&context, sizeof(context) );

	 // Change the instruction pointer to our function
	 PROGCTR(context) = (DWORD) SignalFunc;
	 SetThreadContext(hThread, &context);

	 // Resuming the thread forces our function to be called
	 ResumeThread(hThread);
     }

     Sleep( INFINITE );
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]