Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
SecurityCam - Thread.h

Thread.h

Caricato da: Carlduke
Scarica il programma completo

  1. #ifndef THREAD_H
  2. #define THREAD_H
  3.  
  4. #include <iostream>
  5.  
  6. #ifdef _WIN32
  7. #include <Windows.h>
  8. #endif
  9.  
  10. using namespace std;
  11.  
  12. template <class DataType>
  13. class Thread
  14. {
  15.  public :
  16.          Thread(DataType *thread_data = NULL){data = thread_data; running = false; }
  17. #ifdef _WIN32
  18.          DWORD (*ThreadFunction)(LPVOID data);
  19.  
  20.          friend DWORD _stdcall GetThreadFunction(LPVOID param)
  21.          {
  22.                  Thread<DataType> *thread = (Thread<DataType>*)param;
  23.                  return thread->ThreadFunction((void*)thread->data);
  24.          }
  25.  
  26.          void Create()
  27.          {
  28.                  running = true;
  29.  
  30.                  thread_handle = CreateThread(NULL,0,GetThreadFunction,this,0,&thread_id);
  31.  
  32.                  if(thread_handle == NULL)
  33.                  {
  34.                          cout<<"Failed to create thread!"<<endl;
  35.                          return;
  36.                  }
  37.          }
  38.  
  39.          void Close()
  40.          {
  41.                  CloseHandle(thread_handle);
  42.                  running = false;
  43.          }
  44. #endif
  45.  
  46.          bool is_running(){ return running; }
  47.  
  48.  protected :
  49.  
  50. #ifdef _WIN32
  51.          DWORD thread_id;
  52.          HANDLE thread_handle;
  53. #endif
  54.  
  55.          DataType *data;
  56.          bool running;
  57. };
  58.  
  59. #endif