Jump to content


Photo

C++ Programming : Threads


  • Please log in to reply
1 reply to this topic

#1 linuxuser

linuxuser

    Honourable Member

  • WNSO Addict
  • PipPipPipPip
  • 1,147 posts

Posted 03 September 2004 - 07:24 PM

What are threads?
Simple explanation of threads is - when u open any application such as Internet Explorer, a process is created. For IE it is IExplorer.exe, but in reality, a process is divided into multiple units of process, such units are called threads. When u open Windows Task Manager ,u will see how many threads are running for each process. Right now for IE, i can see 17 threads.
It is important for C/C++ programmer to know what threads are and how they work. Following a C++ class for creating and terminating threads.

:)

#2 linuxuser

linuxuser

    Honourable Member

  • WNSO Addict
  • PipPipPipPip
  • 1,147 posts

Posted 03 September 2004 - 07:25 PM

//thread.h
#include <windows.h>
#include <process.h>
#include <assert.h>
//Class CThread
class CThread
{
public:
CThread( int nPriority = THREAD_PRIORITY_NORMAL );
bool WaitUntilTerminate( DWORD dwMilliSec = INFINITE );
bool Start();
bool Pause();
bool IsRunning();
bool IsTerminated();
bool IsSuspend();
void SetPriority( int nLevel );
int GetPriority();
void Terminate();
public:
virtual void OnInitInstance(){}//main initialization code
virtual void OnRunning() = 0;
virtual DWORD OnExitInstance() { return 0;}//clean up
static unsigned __stdcall ThreadProc( LPVOID param );
virtual ~CThread(){
::CloseHandle( m_hEvent );
}
public:
HANDLE m_hThread, m_hEvent;
int m_nPriority;
unsigned long m_dwThreadID;
bool m_bTerminate, m_bSuspend, m_bIsRunning;

};

//Implementation
CThread::CThread( int nPriority )
{
m_nPriority = nPriority;
m_hThread = NULL;
m_dwThreadID = 0;
m_bTerminate = true;
m_bSuspend = true;
m_bIsRunning = false;
m_hEvent = ::CreateEvent( NULL, FALSE, FALSE, NULL );
}
bool CThread::WaitUntilTerminate( DWORD dwMilliSec )
{
if( m_dwThreadID == 0 ) return false;
if( ::WaitForSingleObject( m_hThread, dwMilliSec ) == WAIT_TIMEOUT ){
return false;
}
m_bIsRunning = false;
m_bTerminate = true;
return true;
}

bool CThread::Start()
{
if( m_bTerminate ){
m_hThread = (HANDLE)CreateThread( NULL, 0, (unsigned long ( __stdcall*)(void*))ThreadProc, (void*)this,0, &m_dwThreadID );
if( m_hThread == 0 ){
return false;
}
else {
::SetEvent( m_hEvent );
m_bIsRunning = true;
return true;
}
}
return true;
}
bool CThread::Pause()
{
if( m_bTerminate ) return false;
if( m_bSuspend ) return true;
DWORD nRet = ::SuspendThread(m_hThread);
if(nRet == 0xFFFFFFFF) return false;
m_bSuspend = true;
return true;
}
bool CThread::IsRunning()
{
return m_bIsRunning;
}

bool CThread::IsTerminated()
{
return m_bTerminate;
}


bool CThread::IsSuspend()
{
return m_bSuspend;
}

void CThread::SetPriority( int nLevel )
{
SetThreadPriority( m_hThread, nLevel );
}

int CThread::GetPriority()
{
return GetThreadPriority( m_hThread );
}
void CThread::Terminate()
{
DWORD dwExitCode;
::GetExitCodeThread( m_hThread, &dwExitCode );
if( dwExitCode == STILL_ACTIVE ){
::TerminateThread( m_hThread, dwExitCode );
::CloseHandle( m_hThread);
m_hThread = NULL;
}
m_bTerminate = true;
m_bIsRunning = false;

}
unsigned __stdcall CThread::ThreadProc( LPVOID param )
{
CThread* pThread = reinterpret_cast<CThread*>(param);
pThread->SetPriority( pThread->m_nPriority );
pThread->m_bIsRunning = true;
::SetEvent( pThread->m_hEvent );
pThread->OnInitInstance();
pThread->OnRunning();
return pThread->OnExitInstance();

}

:)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users