foobar2000 SDK  2015-08-03
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();
39  m_ownNotify = create_task(0);
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:
57  ~CSimpleThread() {AbortThread();}
58 
59  virtual unsigned ThreadProc(abort_callback & p_abort) = 0;
61  virtual void ThreadDone(unsigned p_code) {};
62 private:
63  void CloseThread() {
65  orphan_all_tasks();
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 
84  PFC_CLASS_NOT_COPYABLE_EX(CSimpleThread);
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:86
t_size getOptimalWorkerThreadCount()
Definition: threads.cpp:17
bool IsThreadActive() const
Definition: fb2k_threads.h:49
bool isActive() const
Definition: threads.cpp:70
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
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:106
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
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