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