foobar2000 SDK  2015-01-14
fb2k_threads.h
Go to the documentation of this file.
1 inline static t_size GetOptimalWorkerThreadCount() throw() {
3 }
4 
6 class CVerySimpleThread : private pfc::thread {
7 public:
8  void StartThread(int priority) {
9  this->pfc::thread::startWithPriority( priority );
10  }
11  void StartThread() {
13  }
14 
15  bool IsThreadActive() const {
16  return this->pfc::thread::isActive();
17  }
20  }
21 protected:
23  virtual void ThreadProc() = 0;
24 private:
25 
26  void threadProc() {
27  this->ThreadProc();
28  }
29 
30  PFC_CLASS_NOT_COPYABLE_EX(CVerySimpleThread)
31 };
32 
35 public:
36  void StartThread(int priority) {
37  AbortThread();
38  m_abort.reset();
40  this->pfc::thread::startWithPriority( priority );
41  }
42  void StartThread() {
44  }
45  void AbortThread() {
46  m_abort.abort();
47  CloseThread();
48  }
49  bool IsThreadActive() const {
50  return this->pfc::thread::isActive();
51  }
53  CloseThread();
54  }
55 protected:
58 
59  virtual unsigned ThreadProc(abort_callback & p_abort) = 0;
61  virtual void ThreadDone(unsigned p_code) {};
62 private:
63  void CloseThread() {
66  }
67 
68  void on_task_completion(unsigned p_id,unsigned p_status) {
69  if (IsThreadActive()) {
70  CloseThread();
71  ThreadDone(p_status);
72  }
73  }
74  void threadProc() {
75  unsigned code = ~0;
76  try {
77  code = ThreadProc(m_abort);
78  } catch(...) {}
79  if (!m_abort.is_aborting()) m_ownNotify->on_completion_async(code);
80  }
81  abort_callback_impl m_abort;
83 
85 };
86 
void CloseThread()
Definition: fb2k_threads.h:63
void StartThread(int priority)
Definition: fb2k_threads.h:8
void startWithPriority(int priority)
Definition: threads.cpp:84
t_size getOptimalWorkerThreadCount()
Definition: threads.cpp:17
bool IsThreadActive() const
Definition: fb2k_threads.h:49
bool isActive() const
Definition: threads.cpp:72
PFC_CLASS_NOT_COPYABLE_EX(CSimpleThread)
void WaitTillThreadDone()
Definition: fb2k_threads.h:18
void StartThread(int priority)
Definition: fb2k_threads.h:36
void StartThread()
Definition: fb2k_threads.h:42
void WaitTillThreadDone()
Definition: fb2k_threads.h:52
IMPORTANT: all classes derived from thread must call waitTillDone() in their destructor, to avoid object destruction during a virtual function call!
Definition: threads.h:11
size_t t_size
Definition: int_types.h:48
virtual void ThreadDone(unsigned p_code)
Called when the thread has completed normally, with p_code equal to ThreadProc retval. Not called when AbortThread() or WaitTillThreadDone() was used to abort the thread / wait for the thread to finish.
Definition: fb2k_threads.h:61
completion_notify_ptr create_task(unsigned p_id)
Helper base class for classes that manage nonblocking tasks and get notified back thru completion_not...
void AbortThread()
Definition: fb2k_threads.h:45
IMPORTANT: all classes derived from CSimpleThread must call AbortThread()/WaitTillThreadDone() in the...
Definition: fb2k_threads.h:34
void waitTillDone()
Definition: threads.h:21
static int currentPriority()
Definition: threads.cpp:104
static t_size GetOptimalWorkerThreadCount()
Definition: fb2k_threads.h:1
IMPORTANT: all classes derived from CVerySimpleThread must call WaitTillThreadDone() in their destruc...
Definition: fb2k_threads.h:6
void on_task_completion(unsigned p_id, unsigned p_status)
Definition: fb2k_threads.h:68
virtual void ThreadProc()=0
virtual unsigned ThreadProc(abort_callback &p_abort)=0
completion_notify_ptr m_ownNotify
Definition: fb2k_threads.h:82
bool IsThreadActive() const
Definition: fb2k_threads.h:15
void threadProc()
Definition: fb2k_threads.h:74
abort_callback_impl m_abort
Definition: fb2k_threads.h:81