- now the mess with threads has gone away
- we have a class BaseThread -- this is a base class -- we can inherit from it when creating a new thread - others treads are correctly stopped (when signal comes) -- pthread_join - we have a special thread only for signals git-svn-id: svn://ttmath.org/publicrep/winix/trunk@685 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
91
core/basethread.h
Executable file
91
core/basethread.h
Executable file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2010, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfilecmslucorebasethread
|
||||
#define headerfilecmslucorebasethread
|
||||
|
||||
#include <pthread.h>
|
||||
#include "synchro.h"
|
||||
|
||||
|
||||
|
||||
class BaseThread
|
||||
{
|
||||
public:
|
||||
|
||||
BaseThread();
|
||||
|
||||
// these method must be called before starting the thread
|
||||
void SetSynchro(Synchro * psynchro);
|
||||
void SetSignal(pthread_cond_t * psignal);
|
||||
|
||||
// starting the thread
|
||||
bool StartThread();
|
||||
|
||||
// waiting until the thread exits
|
||||
void WaitForThread();
|
||||
|
||||
|
||||
|
||||
// virtual methods which should/can be inherited by your class
|
||||
// the methods will be called from the other thread
|
||||
|
||||
// initialize the thread
|
||||
// (global objects are locked)
|
||||
// if it returns false then the thread immediately exists
|
||||
virtual bool Init() { return false; }
|
||||
|
||||
// uninitialize the thread
|
||||
// this is called before the thread is prepare to detach
|
||||
// (global objects are locked)
|
||||
virtual void Uninit() {}
|
||||
|
||||
// signal came
|
||||
// check specific job and return true to call Do() next
|
||||
// (global objects are locked -- copy some global objects to local variables)
|
||||
virtual bool SignalReceived() { return false; };
|
||||
|
||||
// if SignalReceived() returned true then this method is called
|
||||
// global object are *not* locked -- use only your local variables
|
||||
// if you have to do something on global objects use synchro->Lock() and synchro->Unlock()
|
||||
virtual void Do() {}
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
// this pointers must be set
|
||||
Synchro * synchro;
|
||||
pthread_cond_t * thread_signal;
|
||||
|
||||
pthread_t thread_id; // thread id - set automatically
|
||||
|
||||
void StartRoutine();
|
||||
static void * StartRoutine(void *);
|
||||
bool BaseInit();
|
||||
void BaseUninit();
|
||||
bool BaseSignalReceived();
|
||||
void BaseDo();
|
||||
bool WaitForSignal();
|
||||
bool WaitForSignalSleep(time_t second);
|
||||
|
||||
bool Lock();
|
||||
void Unlock();
|
||||
|
||||
// if the work done by Do() is long time consuming you should periodically check
|
||||
// wheter there was a signal for exiting, and if it was just simply return from Do()
|
||||
// (it's checking with locking and unlocking)
|
||||
bool IsExitSignal();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user