CAMThread is an abstract class, a worker thread class that provides creation, synchronization, and communication with a worker thread. The worker thread can be accessed from several client threads. The class provides member functions to create the thread, pass commands to it, and wait for it to exit.
Use a CCritSec object to ensure that only one thread can make a request at a time. Use two CAMEvent objects: one to signal to the worker that a request is outstanding, and the other to signal to the client thread that the request has been completed. A nonblocking CAMThread::CheckRequest member function allows the worker thread to check for new requests while working asynchronously.
Derive from this class to provide your own thread member function. You might also want to provide type-safe signaling member functions that package parameters and return values using the CAMThread::CallWorker member function.
Thread creation is independent of object creation. Create a member variable derived from CAMThread, and then use the member functions to start and stop the thread when needed.
Data Members
Name | Description |
m_AccessLock | Critical section object that locks access by client threads. |
m_WorkerLock | Critical section object that locks access to shared objects. |
Member Functions
Name | Description |
CallWorker | Makes a request to the worker thread. |
CheckRequest | Determines if there is an outstanding request. This is a nonblocking member function. |
Close | Blocks until the thread has exited and released its resources. |
Create | Starts the thread running. |
CAMThread | Constructs a CAMThread object. |
GetRequest | Blocks until the next request is made and then returns a DWORD value. |
GetRequestHandle | Returns an event handle. |
GetRequestParam | Returns the latest request. |
InitialThreadProc | Retrieves a this pointer. Carry out this member function before calling the CAMThread::ThreadProc member function. |
Reply | Returns a DWORD value to the requesting thread and releases it, signaling completion of the request. |
ThreadExists | Determines whether a thread exists or has exited. |
ThreadProc | Indicates a pure virtual member function that is called on the worker thread. |
Makes a request to the worker thread and blocks for a response.
DWORD CallWorker(
DWORD dw
);
Returns a value that is defined by the derived class.
This member function uses a CCritSec object to ensure that only one request is made at a time. It is therefore not valid to call the CAMThread::CallWorker member function from the thread itself or from any member function that is executing in the context of the thread.
Determines if there is an outstanding request. This is a nonblocking member function.
BOOL CheckRequest(
DWORD *pParam
);
Returns TRUE if an outstanding request is still active, or FALSE is no request is active.
If there is an outstanding request, the requesting thread will block until the CAMThread::GetRequest member function is called. The request remains outstanding (that is, this member function continues to return TRUE) until either the CAMThread::Reply or CAMThread::GetRequest member function is called.
Blocks until the thread has exited and released its resources.
void Close(void);
No return value.
You must instruct the thread to exit by some other means; for example, call the CAMThread::CallWorker member function with a request that is interpreted by the derived class to mean complete and exit.
If the thread is still running when the CAMThread object is destroyed, the CAMThread::Close member function is called internally.
Starts the thread running.
BOOL Create(void);
Returns TRUE if the thread started successfully, or FALSE if the thread is already running.
This member function creates the thread and calls the CAMThread::ThreadProc member function from the derived class.
Constructs a CAMThread object.
CAMThread( );
No return value.
Creates a CAMThread object but does not create an actual thread. You call the CAMThread::Create member function to create a thread.
Blocks until the next request is made.
DWORD GetRequest( );
Returns a value that is defined by the derived class.
This member function blocks the requesting thread until the CAMThread::Reply function is called.
Returns an event handle for performance improvements.
HANDLE GetRequestHandle( ) const;
Returns an event handle.
To use the Microsoft Win32 WaitForMultipleObjects function, you will need this handle in the thread's wait list or the thread will not be responsive.
Returns the most recent request.
DWORD GetRequestParam( ) const;
Returns a DWORD value that indicates the request made previously by the CAMThread::GetRequest member function.
Receives a this pointer and calls the CAMThread::ThreadProc member function.
DWORD InitialThreadProc(
LPVOID pv
);
Returns the DWORD returned by CAMThread::ThreadProc. This DWORD is not defined by this class.
Returns a DWORD value to the requesting thread and releases it, signaling completion of the request.
void Reply(
DWORD dw
);
No return value.
Determines whether the thread has been created and has not yet exited.
BOOL ThreadExists( );
Returns TRUE if the thread exists and hasn't exited, or FALSE if the thread doesn't exist.
Overridden member function in which to implement a thread.
virtual DWORD ThreadProc( );
The meaning of this return value is not defined by the CAMThread class.
The thread calls this member function upon startup. Derived classes must override this member function. When this member function returns, the thread terminates. This member function is protected.
© 1997 Microsoft Corporation. All rights reserved. Terms of Use.