Event Kernel Object.
There are two different types of event objects: manual-reset events and auto-reset events.
When a manual-reset event is signaled, all threads waiting on the event become schedulable.
When an auto-reset event is signaled, only one of the threads waiting on the event becomes schedulable.
Events are most commonly used when one thread performs initialization work and then signals another thread to perform the remaining work.
The event is initialized as nonsignaled, and then after the thread completes its initial work, it sets the event to signaled.
At this point, another thread, which has been waiting on the event, sees that the event is signaled and becomes schedulable.
This second thread knows that the first thread has completed its work.
For example, you in your mobile application, you are getting an incoming call.
You have created an event for that.
There are set of WIN32 APIs for event kernel object.
CreateEvent(...); // API to Create Event
OpenHandle(...); // API to get Event Handles
SetEvent(...); // API to put an event in signaled state.
ResetEvent(...); // API to put an event in non signaled state.
CloseHandle(...); // API to close Event Handles
One Practicle example: In our mobile application, when we are on call, (incoming, outgoing, 2nd call (one call is already on hold etc). You need to write such functionality into thread. so, that while one call is in process, user can not accept another call before finishing that call or put it on hold. but all these things to be properly designed else you may loose control on your functionality.
First of all, you need to create specific threads and events.
// Create an Auto Reset Event which automatically reset to Non Signalled state after being signalled
IncomingCallEvent
// Create a Thread Which will wait for the events to occur
ON_CALL (here switch case....incoming call, outgoing call, 2nd call (one call is already on hold) etc....you must have specified)
// Signal the event
SetEvent ( hEvent ); (pass appropriate event based on switch-case ...here guass you are passing IncomingCallEvent)
// Wait for the Thread to Die
WaitForSingleObject ( hThrd, INFINITE ); and then close both handle (event and thread)// Standard include headers
#include <windows.h>
#include <iostream> u
sing namespace std;
DWORD WINAPI ON_CALL ( LPVOID n )
{
cout<<"Thread Instantiated........."<<endl;
// Get the handler to the event for which we need to wait in // this thread.
HANDLE hEvent = OpenEvent ( EVENT_ALL_ACCESS , false, "IncomingCallEvent" );
if ( !hEvent ) { return -1; }
// wait for an event to occur
// Wait for the Event
WaitForSingleObject ( hEvent, INFINITE );
// No need to Reset the event as its become non signaled as soon as
// some thread catches the event.
cout<<"Got The signal......."<<endl;
CloseHandle(hEvent);
cout<<"End of the CALL......"<<endl;
return 0;
}
int main()
{
// Create an Auto Reset Event which automatically reset to
// Non Signalled state after being signalled
HANDLE hEvent = CreateEvent ( NULL , false , false , "IncomingCallEvent" );
if ( !hEvent ) return -1;
// Create a Thread Which will wait for the events to occur
DWORD Id;
HANDLE hThrd = CreateThread ( NULL, 0, (LPTHREAD_START_ROUTINE)ON_CALL ,0,0,&Id );
if( !hThrd )
{
CloseHandle (hEvent); return -1;
}
// Signal the event
SetEvent ( hEvent );
Wait for the Thread to Die
WaitForSingleObject ( hThrd, INFINITE );
CloseHandle ( hThrd );
CloseHandle ( hEvent );
cout<<"End of Main ........"<<endl;
return 0;
}
One more example I can explain. When we are writing word document, that time we have grammer check, spelling check, word count check functionality.
such functionality if we want to write then we can create seperate threads for respected functionalities and set appropriate event.
It can be either manual reset or auto rest as per the requirement.
Ideally, they all threads will get CPU time and access the memory block. Notice that all three threads will access the memory in a read-only fashion. This is the only reason why all three threads can run simultaneously.
Friday, April 23, 2010
Thread Synchronization With Kernel Objects : 2
Labels:
Programming Application Concepts,
Thread
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment