4.06.2013 - Version 0.3.4 - PIOBJECT() macro, ethernet improvement, documentation based on Doxygen
This commit is contained in:
47
pithread.cpp
47
pithread.cpp
@@ -31,6 +31,36 @@ void piUSleep(int usecs) {
|
||||
}
|
||||
|
||||
|
||||
/*! \class PIThread
|
||||
* \brief Thread class
|
||||
* \details This class allow you exec your code in separate thread.
|
||||
*
|
||||
* \section PIThread_sec0 Synopsis
|
||||
* Multithread .
|
||||
*
|
||||
* \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.
|
||||
*
|
||||
* \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().
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
PIThread::PIThread(void * data, ThreadFunc func, bool startNow, int timer_delay): PIObject() {
|
||||
piMonitor.threads++;
|
||||
data_ = data;
|
||||
@@ -58,12 +88,14 @@ PIThread::~PIThread() {
|
||||
#ifndef WINDOWS
|
||||
pthread_cancel(thread);
|
||||
#else
|
||||
TerminateThread(thread, 0);
|
||||
CloseHandle(thread);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
bool PIThread::start(int timer_delay) {
|
||||
if (running) return false;
|
||||
terminating = running = false;
|
||||
timer = timer_delay;
|
||||
#ifndef WINDOWS
|
||||
@@ -88,6 +120,7 @@ bool PIThread::start(int timer_delay) {
|
||||
|
||||
|
||||
bool PIThread::startOnce() {
|
||||
if (running) return false;
|
||||
terminating = running = false;
|
||||
#ifndef WINDOWS
|
||||
pthread_attr_t attr;
|
||||
@@ -110,13 +143,13 @@ bool PIThread::startOnce() {
|
||||
}
|
||||
|
||||
|
||||
void PIThread::terminate(bool hard) {
|
||||
void PIThread::terminate() {
|
||||
if (thread == 0) return;
|
||||
running = false;
|
||||
#ifndef WINDOWS
|
||||
if (hard) kill((ullong)thread, SIGKILL);
|
||||
else pthread_cancel(thread);
|
||||
pthread_cancel(thread);
|
||||
#else
|
||||
TerminateThread(thread, 0);
|
||||
CloseHandle(thread);
|
||||
#endif
|
||||
thread = 0;
|
||||
@@ -125,6 +158,10 @@ void PIThread::terminate(bool hard) {
|
||||
|
||||
|
||||
void * PIThread::thread_function(void * t) {
|
||||
#ifndef WINDOWS
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
||||
#endif
|
||||
PIThread & ct = *((PIThread * )t);
|
||||
ct.running = true;
|
||||
ct.begin();
|
||||
@@ -150,6 +187,10 @@ void * PIThread::thread_function(void * t) {
|
||||
|
||||
|
||||
void * PIThread::thread_function_once(void * t) {
|
||||
#ifndef WINDOWS
|
||||
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
|
||||
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
|
||||
#endif
|
||||
PIThread & ct = *((PIThread * )t);
|
||||
ct.running = true;
|
||||
ct.begin();
|
||||
|
||||
Reference in New Issue
Block a user