19 #include "threadexceptions.h"
30 this->user_thread_function=NULL;
38 if(user_thread_function==NULL)
46 this->user_thread_function=user_thread_function;
58 if(this->user_thread_function==NULL)
62 throw CThreadException(_HERE_,
"The thread has not been attached to a function",this->thread_id);
66 if((error=pthread_create(&this->thread,NULL,this->
thread_function,
this))!=0)
70 throw CThreadException(_HERE_,
"Impossible to start the thread",this->thread_id);
85 if(this->state==active || this->state==starting)
88 if((error=pthread_join(this->thread,NULL))!=0)
91 throw CThreadException(_HERE_,
"Error while waiting the thread to end",this->thread_id);
104 this->access.
enter();
105 if(this->state==active || this->state==starting)
108 if((error=pthread_cancel(this->thread))!=0)
112 throw CThreadException(_HERE_,
"Error while cancelling the thread",this->thread_id);
115 if((error=pthread_join(this->thread,NULL))!=0)
118 throw CThreadException(_HERE_,
"Error while waiting the thread to end",this->thread_id);
120 this->access.
enter();
121 this->state=attached;
128 this->access.
enter();
129 if(this->state==attached)
131 this->user_thread_function=NULL;
133 this->state=detached;
143 this->access.
enter();
144 tmp_state=this->state;
163 return this->thread_id;
170 thread->access.
enter();
171 thread->state=active;
172 thread->access.
exit();
173 thread->user_thread_function(thread->param);
static void * thread_function(void *param)
Internal thread function.
Implementation of a parallel thread of execution.
std::string get_id(void)
Function to get the thread identifier.
void attach(void *(*user_thread_function)(void *param), void *param)
Function to attach the user function of the thread.
virtual ~CThread()
Destructor.
int get_state(void)
Function to retrieve the state of the thread.
void end(void)
Function to request the end of the execution of the thread.
void detach(void)
Function to remove the assigned user function.
void set_id(const std::string &id)
Function to set the thread identifier.
void exit(void)
function to release the critical section
void enter(void)
function to request access to the critical section
void kill(void)
Function to immediatelly terminate the execution of the thread.
void start(void)
Function to start the execution of the thread.
CThread(const std::string &id)
Constructor.