Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

C++ And threading

Started by necreia Oct 26, 2005 at 7:58 PM 6 replies 850+ views
Original Post
necreia
necreia
I'm looking for a good/simple threading tutorial for Windows/C++. I have found this, however I don't understand "how" the thread will be instantiated with those directions. Setup() doesn't call Start(). I'd like to compare it to another example/tutorial. Thanks in advanced.
rmsimpson
rmsimpson
Here's a pretty simple worker thread base class. To use it, derive a class from it, and implement the Run() function. If you return TRUE, the thread keeps calling Run(), and if you return FALSE the thread will exit.

class CWorkerBase{protected:  HANDLE m_hThread;    // Handle to the thread  DWORD  m_dwThreadId; // Thread IDprivate:  BOOL   m_bTerminate; // Signal to terminatepublic:  CWorkerBase()  {    // Initialize and create the worker thread    m_bTerminate = FALSE;    m_hThread = CreateThread(NULL, 0, _WorkerThreadProc, this, 0, &m_dwThreadId);  }  // Shutdown the worker thread  virtual ~CWorkerBase()  {    if (m_hThread != NULL)    {      m_bTerminate = TRUE;      // Wait 3 seconds for the thread to finish cleanly      if (IsFinished(3000) == FALSE) // The thread never quit voluntarily      {        // TODO: Take drastic measures perhaps?        TerminateThread(m_hThread, MAXDWORD);      }      CloseHandle(m_hThread);    }  }    // Returns TRUE if the thread is finished doing its work.  // If dwWaitMS is 0, it returns immediately, otherwise it will wait  // that many milliseconds for the thread to complete its work.  It will  // not tell the thread to exit, it'll just see if its done.  Use INFINITE  // if you want a guaranteed wait until the thread is finished.  BOOL IsFinished(DWORD dwWaitMS = 0)  {    if (m_hThread == NULL) return TRUE;    else return (WaitForSingleObject(m_hThread, dwWaitMS) == WAIT_OBJECT_0);  }protected:  virtual BOOL Run() = 0; // Return TRUE to keep on executingprivate:  static DWORD WINAPI _WorkerThreadProc(LPVOID pvThis)  {    CWorkerBase *p = static_cast<CWorkerBase *>(pvThis);    // Keep calling Run() over and over again until it returns FALSE or    // the class is being destroyed    while (p->Run() == TRUE && p->m_bTerminate == FALSE);    ExitThread(0);  }};


Here's a sample derived class

class CWorker : public CWorkerBase{public:  BOOL Run()  {    printf("Hello world!\r\n");    return FALSE; // Only run this once.  }};


... and a very simple main() to demonstrate it
int main(int argc, char* argv[]){  CWorker worker;  printf("Worker created\r\n");  return 0;}
MaulingMonkey
MaulingMonkey
If you have Boost, check out Boost.Threads.

If you don't have Boost, I'm not talking to you. :P.

(Seriously though, boost FTW. You can probably guess what this will do:)

#include <boost/bind.hpp> //boost::bind#include <boost/threads.hpp> //boost::thread#include <iostream> //std::cout#include <string> //std::string#include <windows.h> //Sleepvoid sleep_for_then_print( DWORD ms , const std::string & argument ) {    Sleep( ms );    std::cout << argument << std::endl;}int main () {    boost::thread second1( boost::bind( &sleep_for_then_print , 1000 , "Seconds Elapsed: 1" ) );    boost::thread second2( boost::bind( &sleep_for_then_print , 2000 , "Seconds Elapsed: 2" ) );    boost::thread second3( boost::bind( &sleep_for_then_print , 3000 , "Seconds Elapsed: 3" ) );    sleep_for_then_print( 4000 , "Seconds Elapsed: 4" );}
necreia
necreia
Quote:
Original post by rmsimpson
...


Perfect! Thank you very much!

I haven't looked into boost... but doing that now.

SanityAssassin
SanityAssassin
Quote:
Original post by rmsimpson
Here's a pretty simple worker thread base class. To use it, derive a class from it, and implement the Run() function. If you return TRUE, the thread keeps calling Run(), and if you return FALSE the thread will exit.

*** Source Snippet Removed ***

Here's a sample derived class

*** Source Snippet Removed ***

... and a very simple main() to demonstrate it
*** Source Snippet Removed ***


What header files do you need to include to use this CWorkerBase class? I've tried including WTypes.h and WinDef.h, but it won't compile.
Nit
Nit
#include
eastcoastsurfer
eastcoastsurfer
I don't have any tutorials handy but if you're going to do threading you need to read up on blocking and semaphores. Just spinning off a worker thread can be useful, but generally you need to share data among threads and that's where it can get complicated.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.