This code demonstrates how to use RegisterForEvent() to install a handler for EVENT_THREAD_SWITCH event. Please note that this handler is called only if your application has 2 or more threads (because it is called only when the control is switches from one to another thread OF YOUR APPLICATION, not just any running thread).
This application spawns two child threads that do nothing but relinquish control, and installs a handler for EVENT_THREAD_SWITCH. Both these threads and the handler increment their counters when they get a chance; by pressing a key, you can view the counters. If you run this sample, you'll see that the number of times the EVENT_THREAD_SWITCH handler is called is roughly two times more than the number of times child thread 1 relinquishes its control.
#include <nwadv.h>
#include <nwconio.h>
#include <nwerrno.h>
#include <nwthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
static int volatile NLM_ThreadCount = 0;
static int volatile NLM_ExitRequested = 0;
static int NLM_MainThreadID;
static int NLM_MainThreadGroupID;
static int ThreadID_1;
static int volatile ThreadCounter_1 = 0;
static int ThreadID_2;
static int volatile ThreadSwitchCount = 0;
static void SignalHandler( int sig_no) {
#ifdef __MWERKS__
#pragma unused( sig_no)
#endif
int saved_thread_group_ID = SetThreadGroupID( NLM_MainThreadGroupID);
signal( SIGTERM, SignalHandler);
NLM_ExitRequested = !0;
ungetch( 27);
while ( NLM_ThreadCount > 0) delay( 100);
SetThreadGroupID( saved_thread_group_ID);
}
static void Thread_1( void) {
++NLM_ThreadCount;
while ( NLM_ExitRequested == 0) {
ThreadSwitch(); /* WithDelay(); */
++ThreadCounter_1;
}
--NLM_ThreadCount;
}
static void Thread_2( void) {
++NLM_ThreadCount;
while ( NLM_ExitRequested == 0) {
ThreadSwitch(); /* WithDelay(); */
}
--NLM_ThreadCount;
}
static void EventProc( unsigned long param, unsigned long user_param) {
#ifdef __MWERKS__
#pragma unused( param, user_param)
#endif
++NLM_ThreadCount;
/* if ( param == ThreadID_1) */ ++ThreadSwitchCount;
--NLM_ThreadCount;
}
int main() {
unsigned long event_handle;
++NLM_ThreadCount;
NLM_MainThreadID = GetThreadID();
NLM_MainThreadGroupID = GetThreadGroupID();
signal( SIGTERM, SignalHandler);
if (( ThreadID_1 = BeginThread(( void( *)( void*)) Thread_1, NULL, 0, NULL)) == EFAILURE) {
puts( "BeginThread 1 failed");
return EXIT_FAILURE;
}
if (( ThreadID_2 = BeginThread(( void( *)( void*)) Thread_2, NULL, 0, NULL)) == EFAILURE) {
puts( "BeginThread 2 failed");
NLM_ExitRequested = !0;
while ( NLM_ThreadCount > 1) delay( 100);
return EXIT_FAILURE;
}
event_handle = RegisterForEvent( EVENT_THREAD_SWITCH, EventProc, NULL);
if ( event_handle == EFAILURE) {
puts( "RegisterForEvent failed");
NLM_ExitRequested = !0;
while ( NLM_ThreadCount > 1) delay( 100);
return EXIT_FAILURE;
}
puts( "Press a key to view counters, <Esc> for exit");
while ( NLM_ExitRequested == 0) {
int chr = getch();
if ( chr == 27) break;
printf( "ThreadSwitchCount = %d, ThreadCounter_1 = %d\n",
ThreadSwitchCount,
ThreadCounter_1);
}
NLM_ExitRequested = !0;
UnregisterForEvent( event_handle);
while ( NLM_ThreadCount > 1) delay( 100);
--NLM_ThreadCount;
return EXIT_SUCCESS;
}
© 2008 Novell, Inc. All Rights Reserved.