20.10.2013 - Modified PIObject - virtual debugName() for macro piCoutObj, improved timer measurements and timers on Windows
This commit is contained in:
62
pithread.cpp
62
pithread.cpp
@@ -36,27 +36,47 @@ void piUSleep(int usecs) {
|
||||
* \details This class allow you exec your code in separate thread.
|
||||
*
|
||||
* \section PIThread_sec0 Synopsis
|
||||
* Multithread .
|
||||
* Multithreading allow you to write program which will be executed
|
||||
* in several threads simultaneously. This trend allow you to use all
|
||||
* cores of modern processors, but there are many dangers.
|
||||
*
|
||||
* \section PIThread_sec1 To/from data convertions
|
||||
* Most common constructor is \a PIThread(const char * str), where "str"
|
||||
* is null-terminated string, e.g. \c "string". This is 7 chars with last char = 0.
|
||||
* Also you can constructs \a PIThread from single \a PIChar, \a PIByteArray,
|
||||
* other \a PIThread or sequency of the same characters with custom length.\n \n
|
||||
* This class has implicit conversions to <tt>const char * </tt> and
|
||||
* \c std::string. Also there are functions to make same convertions:
|
||||
* * \a data() - to <tt>const char * </tt>,
|
||||
* * \a stdString() - to \c std::string,
|
||||
* * \a toByteArray() - to \a PIByteArray.
|
||||
* This class provide virtual functions \a begin(), \a run() and \a end(),
|
||||
* which describes start, execution and finish work of some process.
|
||||
* These functions executes in \b separate thread. When you execute
|
||||
* \a start(), %PIThread create separate system thread and sequentially
|
||||
* executes function \a begin(), \a run() and \a end(). You can
|
||||
* reimplement each function and write your own code to execute.
|
||||
* Scheme of functions executing:
|
||||
\code{.cpp}
|
||||
begin();
|
||||
event started();
|
||||
while (isRunning()) {
|
||||
run();
|
||||
ThreadFunc();
|
||||
msleep(timer_delay);
|
||||
}
|
||||
event stopped();
|
||||
end();
|
||||
\endcode
|
||||
* Unlike from directly using "pthread" or some similar you doesn`t need
|
||||
* to write your own main thread cycle and sleep at every cycle end.
|
||||
* %PIThread make it for you, and your job is to set sleep value from
|
||||
* contructor or when starting thread, and reimplement \a begin(), \a run()
|
||||
* and \a end() functions.
|
||||
*
|
||||
* \section PIThread_sec2 Numeric operations
|
||||
* You can get symbolic representation of any numeric value with function
|
||||
* \a setNumber(any integer value, int base = 10, bool * ok = 0). Default
|
||||
* arguments are set for decimal base system, but you can choose any system
|
||||
* from 2 to 40. There are the same static functions \a fromNumber(), that
|
||||
* returns \a PIThread. \n
|
||||
* Also there is function \a setReadableSize() which is set human-readable
|
||||
* size in bytes, Kb, Mb, Gb or Pb. Static analog is \a readableSize().
|
||||
* \section PIThread_sec1 Using without subclassing
|
||||
* You can use %PIThread without subclassing by using "ThreadFunc" pointer
|
||||
* that can be set from constructor or by overloaded function \a start(ThreadFunc func, int timer_delay).
|
||||
* If "func" if not null this function will be executed as \a run(). ThreadFunc is any static
|
||||
* function with format void func(void * data). "Data" is custom data set from constructor or
|
||||
* with function \a setData(). \n Also you can connect to event \a started(), but
|
||||
* in this case you should to white your thread main cycle, because this event raised only one time.
|
||||
*
|
||||
* \section PIThread_sec2 Locking
|
||||
* %PIThread has inrternal mutex that can be locked and unlocked every \a run() if you set this flag
|
||||
* with function \a needLockRun(bool). Also you can access to this mutex by functions \a lock(), \a unlock()
|
||||
* and \a mutex(). Using this functions together with needLockRun(true) can guarantee one-thread access to
|
||||
* some data.
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -163,6 +183,8 @@ void * PIThread::thread_function(void * t) {
|
||||
#ifndef WINDOWS
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
||||
#else
|
||||
__PISetTimerResolution();
|
||||
#endif
|
||||
PIThread & ct = *((PIThread * )t);
|
||||
ct.running = true;
|
||||
@@ -192,6 +214,8 @@ void * PIThread::thread_function_once(void * t) {
|
||||
#ifndef WINDOWS
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
||||
#else
|
||||
__PISetTimerResolution();
|
||||
#endif
|
||||
PIThread & ct = *((PIThread * )t);
|
||||
ct.running = true;
|
||||
|
||||
Reference in New Issue
Block a user