Operator Console
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
ThreadControl Class Reference

Thread synchronization and communication class. More...

#include <ThreadControl.h>

Public Member Functions

 ThreadControl ()
 
virtual ~ThreadControl (void)
 
bool Init (UINT threadMsg, DWORD parentID, AFX_THREADPROC threadProc, void *data)
 
void Quit ()
 
void Run ()
 
 ThreadControl ()
 
virtual ~ThreadControl (void)
 
bool Init (UINT threadMsg, DWORD parentID, AFX_THREADPROC threadProc, void *data)
 
void Quit ()
 
void Run ()
 

Public Attributes

CWinThread * m_thread
 the thread that we want to control More...
 
CEvent * m_runEvent
 when this event is received, run the child's function once More...
 
CEvent * m_doneEvent
 when this event is received, terminate the thread More...
 
HANDLE m_events [2]
 
UINT m_threadMsg
 message to send to parent thread More...
 
DWORD m_parentID
 the thread to send the message to More...
 
void * m_data
 pointer to whatever is useful More...
 

Protected Member Functions

void InitEvents ()
 
bool InitThread (AFX_THREADPROC threadProc)
 
void InitEvents ()
 
bool InitThread (AFX_THREADPROC threadProc)
 

Detailed Description

Thread synchronization and communication class.

This class simplifies the task of having 2 threads communicate with each other.

Thread control is done using CEvent objects, by calling WaitForMultipleObjects(). The child thread will block until it receives either of 2 objects: RUN_OBJECT tells the thread to run its function, and send a message to its parent thread DONE_OBJECT tells the thread to quit

A pointer to a ThreadControl object should be passed as the thread function's single parameter

Constructor & Destructor Documentation

ThreadControl::ThreadControl ( )

References DONE_INDEX, m_data, m_doneEvent, m_events, m_runEvent, m_thread, and RUN_INDEX.

24 {
25  m_thread = NULL;
26  m_data = NULL;
27  m_runEvent = new CEvent(FALSE, FALSE);
28  m_doneEvent = new CEvent(FALSE, FALSE);
29  m_events[RUN_INDEX] = m_runEvent->m_hObject;
30  m_events[DONE_INDEX] = m_doneEvent->m_hObject;
31 }
void * m_data
pointer to whatever is useful
Definition: ThreadControl.h:77
HANDLE m_events[2]
Definition: ThreadControl.h:74
CWinThread * m_thread
the thread that we want to control
Definition: ThreadControl.h:71
CEvent * m_doneEvent
when this event is received, terminate the thread
Definition: ThreadControl.h:73
#define RUN_INDEX
Definition: ThreadControl.h:49
CEvent * m_runEvent
when this event is received, run the child's function once
Definition: ThreadControl.h:72
#define DONE_INDEX
Definition: ThreadControl.h:50
ThreadControl::~ThreadControl ( void  )
virtual

References m_doneEvent, m_runEvent, and m_thread.

35 {
36  if (m_runEvent != NULL)
37  {
38  delete m_runEvent;
39  }
40 
41  if (m_doneEvent != NULL)
42  {
43  delete m_doneEvent;
44  }
45 
46  if (m_thread != NULL)
47  {
48  delete m_thread;
49  }
50 }
CWinThread * m_thread
the thread that we want to control
Definition: ThreadControl.h:71
CEvent * m_doneEvent
when this event is received, terminate the thread
Definition: ThreadControl.h:73
CEvent * m_runEvent
when this event is received, run the child's function once
Definition: ThreadControl.h:72
ThreadControl::ThreadControl ( )
virtual ThreadControl::~ThreadControl ( void  )
virtual

Member Function Documentation

bool ThreadControl::Init ( UINT  threadMsg,
DWORD  parentID,
AFX_THREADPROC  threadProc,
void *  data 
)

References InitThread(), m_data, m_parentID, and m_threadMsg.

Referenced by COperatorConsoleApp::InitBlemishThread(), COperatorConsoleApp::InitCameraThread(), and COperatorConsoleApp::InitSFRplusThread().

53 {
54  bool success;
55 
56  m_threadMsg = threadMsg;
57  m_parentID = parentID;
58  m_data = data;
59 
60  success = InitThread(threadProc);
61 
62  return success;
63 }
void * m_data
pointer to whatever is useful
Definition: ThreadControl.h:77
bool InitThread(AFX_THREADPROC threadProc)
Definition: ThreadControl.cpp:65
DWORD m_parentID
the thread to send the message to
Definition: ThreadControl.h:76
UINT m_threadMsg
message to send to parent thread
Definition: ThreadControl.h:75

Here is the call graph for this function:

Here is the caller graph for this function:

bool ThreadControl::Init ( UINT  threadMsg,
DWORD  parentID,
AFX_THREADPROC  threadProc,
void *  data 
)
void ThreadControl::InitEvents ( )
protected
void ThreadControl::InitEvents ( )
protected
bool ThreadControl::InitThread ( AFX_THREADPROC  threadProc)
protected

References m_thread.

Referenced by Init().

66 {
67  SECURITY_ATTRIBUTES security;
68 
69  security.nLength = sizeof(security);
70  security.lpSecurityDescriptor = NULL;
71  security.bInheritHandle = TRUE;
72 
73  //
74  // Create the thread. We set auto delete to false so that we can tell when the thread
75  // quits (otherwise Windows would delete the thread when threadProc returns, and its handle
76  // would be invalid).
77  //
78  // In order to *safely* set auto delete, we have to create the thread in suspended mode.
79  // This ensures that the thread won't quit before we set auto delete.
80  //
81  m_thread = ::AfxBeginThread(threadProc, this, 0, 0, CREATE_SUSPENDED, &security);//NULL);
82 
83  if (m_thread != NULL)
84  {
85  m_thread->m_bAutoDelete = FALSE;
86  m_thread->ResumeThread();
87  }
88 
89  return m_thread != NULL;
90 }
CWinThread * m_thread
the thread that we want to control
Definition: ThreadControl.h:71

Here is the caller graph for this function:

bool ThreadControl::InitThread ( AFX_THREADPROC  threadProc)
protected
void ThreadControl::Quit ( )
void ThreadControl::Quit ( )

References m_doneEvent, and m_thread.

Referenced by COperatorConsoleApp::Quit(), and COperatorConsoleApp::ReInit().

93 {
94  if (m_thread != NULL)
95  {
96  m_doneEvent->SetEvent(); // trigger the Quit event
97  ::WaitForSingleObject(m_thread->m_hThread, INFINITE); // wait for it to quit
98  delete m_thread; // delete the thread
99  m_thread = NULL;
100  }
101 }
CWinThread * m_thread
the thread that we want to control
Definition: ThreadControl.h:71
CEvent * m_doneEvent
when this event is received, terminate the thread
Definition: ThreadControl.h:73

Here is the caller graph for this function:

void ThreadControl::Run ( )

References m_runEvent.

Referenced by COperatorConsoleApp::OnFrameReady(), and COperatorConsoleApp::OnRunTest().

104 {
105  m_runEvent->SetEvent(); // trigger the Run event
106 }
CEvent * m_runEvent
when this event is received, run the child's function once
Definition: ThreadControl.h:72

Here is the caller graph for this function:

void ThreadControl::Run ( )

Member Data Documentation

void * ThreadControl::m_data
CEvent * ThreadControl::m_doneEvent

when this event is received, terminate the thread

Referenced by Quit(), ThreadControl(), and ~ThreadControl().

HANDLE ThreadControl::m_events
DWORD ThreadControl::m_parentID

the thread to send the message to

Referenced by Init(), ImageAcquisition::ThreadProc(), and ImageTest::ThreadProc().

CEvent * ThreadControl::m_runEvent

when this event is received, run the child's function once

Referenced by Run(), ThreadControl(), and ~ThreadControl().

CWinThread * ThreadControl::m_thread

the thread that we want to control

Referenced by InitThread(), Quit(), ThreadControl(), and ~ThreadControl().

UINT ThreadControl::m_threadMsg

message to send to parent thread

Referenced by Init(), ImageAcquisition::ThreadProc(), and ImageTest::ThreadProc().


The documentation for this class was generated from the following files: