📄 tthread.hh
字号:
#ifndef __TTHREAD_HH#define __TTHREAD_HH//// Project : threadpool// File : TThread.hh// Author : Ronald Kriemann// Purpose : baseclass for a thread-able class//// Copyright (C) 2003 - Ronald Kriemann//// This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU Lesser General Public License for more details.//// You should have received a copy of the GNU Lesser General Public// License along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA//#include <stdio.h>#include <pthread.h>#include "types.hh"#include "TSLL.hh"//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// baseclass for all threaded classes// - defines basic interface//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class TThread{protected: // thread-specific things pthread_t _thread_id; pthread_attr_t _thread_attr; // is the thread running or not bool _running; // no of thread int _thread_no; public: //////////////////////////////////////////// // // constructor and destructor // TThread ( int thread_no = -1 ); virtual ~TThread (); //////////////////////////////////////////// // // access local data // int thread_no () const { return _thread_no; } int proc_no () const { return _thread_no; } void set_thread_no ( int n ); // compare if given proc-no is local one bool on_proc ( int p ) const { return ((p == -1) || (_thread_no == -1) || (p == _thread_no)); } // resets running-status (used in _run_proc, see TThread.cc) void reset_running () { _running = false; } //////////////////////////////////////////// // // user-interface // // actually method the thread executes virtual void run () = 0; // start (stop) thread virtual void start ( bool detached = false, bool sscope = false ) { create( detached, sscope ); } virtual void stop () { cancel(); } //////////////////////////////////////////// // // thread management // // create thread (actually start it) void create ( bool detached = false, bool sscope = false ); // detach thread void detach (); // synchronise with thread (wait until finished) void join (); // terminate thread void exit (); // request cancellation of thread void cancel (); // send signal signo to thread void kill ( int signo ); // put thread to sleep (milli + nano seconds) void sleep ( long m_sec, long nano_sec );};//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// wrapper for pthread_mutex//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class TMutex{ // friends friend class TCondition; protected: // the mutex itself and the mutex-attr pthread_mutex_t _mutex; pthread_mutexattr_t _mutex_attr;public: ///////////////////////////////////////////////// // // constructor and destructor // TMutex () { pthread_mutexattr_init( & _mutex_attr ); pthread_mutex_init( & _mutex, & _mutex_attr ); } ~TMutex () { pthread_mutex_destroy( & _mutex ); pthread_mutexattr_destroy( & _mutex_attr ); } ///////////////////////////////////////////////// // // usual behavior of a mutex // // lock and unlock mutex (return 0 on success) int lock () { return pthread_mutex_lock( & _mutex ); } int unlock () { return pthread_mutex_unlock( & _mutex ); } // try if locked // (return 0 if lock was successful) int trylock () { return pthread_mutex_trylock( & _mutex ); }};//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class for a condition variable//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class TCondition{protected: // our condition variable pthread_cond_t _cond; // attribute of condition variable // pthread_condattr_t _attr; public: ///////////////////////////////////////////////// // // constructor and destructor // TCondition () { pthread_cond_init( & _cond, NULL ); } ~TCondition () { pthread_cond_destroy( & _cond ); } ///////////////////////////////////////////////// // // condition variable related methods // void wait ( TMutex & m ) { pthread_cond_wait( & _cond, & m._mutex ); } void signal () { pthread_cond_signal( & _cond ); } void broadcast () { pthread_cond_broadcast( & _cond ); }};//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class for a semaphore// - when counter > 0 do not lock, else lock and wait// till counter has increased//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class TSemaphore{protected: // local counter int _value; // mutex for local access TMutex _mutex; // condition to wait if counter == 0 TCondition _cond; public: ////////////////////////////////////////// // // constructor and destructor // TSemaphore ( int c = 0 ) : _value(c) {} ~TSemaphore () {} ////////////////////////////////////////// // // access local variables // int value () const { return _value; } ////////////////////////////////////////// // // semaphore behaviour // // reset counter void init ( int c ); // increment counter by c void inc (); // decrement counter by c void dec ();};//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// class for a counter (anti-semaphore)// - when counter != 0 and init or destructor is called// unlocked mutices are _not_ released//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////class TCounter{protected: // local counter int _counter; // list of mutices waiting TSLL< TMutex * > _mutex_list; // mutex for local access TMutex _mutex;public: ////////////////////////////////////////// // // constructor and destructor // TCounter ( int c = -1 ) : _counter(c) {} ~TCounter () {} ////////////////////////////////////////// // // access local variables // int counter () const { return _counter; } ////////////////////////////////////////// // // counter behaviour // // reset counter void init ( int c ); // decrement counter (decrement by d and reinit to c) void dec ( int d = 1, int c = 0 );protected: // release all mutices in list void release_all ();};#endif // __TTHREAD_HH
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -