16 #ifndef TY_THREADING_9613
17 #define TY_THREADING_9613
25 #define TY_AUTO_MUTEX_NAME _mutex
28 #ifndef QT_THREAD_SUPPORT
29 #define QT_THREAD_SUPPORT 1
33 #include <QWaitCondition>
43 static void sleep(
unsigned long secs)
45 return QThread::sleep(secs);
47 static void msleep(
unsigned long msecs)
49 return QThread::msleep(msecs);
51 static void usleep(
unsigned long usecs)
53 return QThread::usleep(usecs);
58 #define TY_AUTO_MUTEX mutable QMutex TY_AUTO_MUTEX_NAME;
59 #define TY_LOCK_AUTO_MUTEX TY_AUTO_MUTEX_NAME.lock();
60 #define TY_UNLOCK_AUTO_MUTEX TY_AUTO_MUTEX_NAME.unlock();
61 #define TY_OMUTEXLOCKER_AUTO_MUTEX OMutexLocker locker(TY_AUTO_MUTEX_NAME);
63 #define TY_MUTEX(name) mutable QMutex name;
64 #define TY_LOCK_MUTEX(name) name.lock();
65 #define TY_UNLOCK_MUTEX(name) name.unlock();
66 #define TY_OMUTEXLOCKER_MUTEX(name) OMutexLocker locker(name);
68 #define TY_STATIC_MUTEX(name) static QMutex name;
69 #define TY_STATIC_MUTEX_INSTANCE(name) QMutex name;
71 #define TY_AUTO_SHARED_MUTEX mutable QMutex* TY_AUTO_MUTEX_NAME;
72 #define TY_NEW_AUTO_SHARED_MUTEX TY_AUTO_MUTEX_NAME = new QMutex();
73 #define TY_DELETE_AUTO_SHARED_MUTEX delete TY_AUTO_MUTEX_NAME;
74 #define TY_LOCK_AUTO_SHARED_MUTEX TY_AUTO_MUTEX_NAME->lock();
75 #define TY_UNLOCK_AUTO_SHARED_MUTEX TY_AUTO_MUTEX_NAME->unlock();
76 #define TY_OMUTEXLOCKER_AUTO_SHARED_MUTEX OMutexLocker locker(*TY_AUTO_MUTEX_NAME);
78 #define TY_SHARED_MUTEX(name) mutable QMutex* name;
79 #define TY_NEW_SHARED_MUTEX(name) name = new QMutex();
80 #define TY_DELETE_SHARED_MUTEX(name) delete name;
81 #define TY_LOCK_SHARED_MUTEX(name) name->lock();
82 #define TY_UNLOCK_SHARED_MUTEX(name) name->unlock();
83 #define TY_OMUTEXLOCKER_SHARED_MUTEX(name) OMutexLocker locker(*name);
114 _mutex =
const_cast<QMutex*
>(&mutex);
224 class OThreadPool :
public std::vector<OSlaveThread*>,
public QWaitCondition,
public QMutex
255 void begin(
unsigned int count);
SmartPtr< OTask > LPOTask
Smart Pointer sur OTask.
#define TY_LOCK_SHARED_MUTEX(name)
#define TY_UNLOCK_SHARED_MUTEX(name)
Class used as RAII object (Resource Acquisition Is Initialization)
~OMutexLocker()
Destructor.
OMutexLocker(QMutex &mutex)
Constructors.
This class defines a thread for running tasks in a threads collection. Slave thread for the threads c...
~OSlaveThread()
Destroy the slave thread; wait for the end of the thread.
OSlaveThread(OThreadPool *pool)
Build a slave thread for a threads collection.
void run()
Run a waiting task.
OThreadPool * _pool
Pointer on the parent threads collection.
Access to sleep protected methods of the QThread class.
static void usleep(unsigned long usecs)
static void msleep(unsigned long msecs)
static void sleep(unsigned long secs)
Task of a threads collection.
bool isCanceled() const
Return true if the task has been cancelled, false otherwise.
void reset()
Reset the task status (_running=false and _completed=false)
bool _running
Running flag.
bool isCompleted() const
Return true if the task is completed, false otherwise.
virtual void main()=0
Main routine of the task. This pure virtual method should be overloaded into an inherited class to de...
bool isRunning() const
Return true if the task is running, false otherwise.
bool _completed
Completed flag.
virtual ~OTask()
Destructor : waits for the end of the task to destroy it.
OTask()
Default constructor.
bool _canceled
Cancel flag.
Slave threads collection.
unsigned int _totalCount
Total number of tasks to run.
unsigned int getTotalCount() const
Return the total number of tasks.
void begin(unsigned int count)
Begin solver.
OThreadPool(unsigned int slaves)
Build a threads collection and allocate "slaves" thread.
virtual ~OThreadPool()
Destructor.
unsigned int _counter
Total number of ended tasks.
std::queue< LPOTask > _tasks
Tasks queue.
unsigned int getCount() const
Return the counter.
void stop()
Cancel the pending tasks.
virtual void push(OTask *task)
Add a task to the queue.