1 // VirtThread.cpp 2 3 #include "StdAfx.h" 4 5 #include "VirtThread.h" 6 CoderThread(void * p)7static THREAD_FUNC_DECL CoderThread(void *p) 8 { 9 for (;;) 10 { 11 CVirtThread *t = (CVirtThread *)p; 12 t->StartEvent.Lock(); 13 if (t->Exit) 14 return 0; 15 t->Execute(); 16 t->FinishedEvent.Set(); 17 } 18 } 19 Create()20WRes CVirtThread::Create() 21 { 22 RINOK(StartEvent.CreateIfNotCreated()); 23 RINOK(FinishedEvent.CreateIfNotCreated()); 24 StartEvent.Reset(); 25 FinishedEvent.Reset(); 26 Exit = false; 27 if (Thread.IsCreated()) 28 return S_OK; 29 return Thread.Create(CoderThread, this); 30 } 31 Start()32void CVirtThread::Start() 33 { 34 Exit = false; 35 StartEvent.Set(); 36 } 37 WaitThreadFinish()38void CVirtThread::WaitThreadFinish() 39 { 40 Exit = true; 41 if (StartEvent.IsCreated()) 42 StartEvent.Set(); 43 if (Thread.IsCreated()) 44 { 45 Thread.Wait(); 46 Thread.Close(); 47 } 48 } 49