General IRI utilities
thread.h
1 // Copyright (C) 2009-2010 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
2 // Author Sergi Hernandez (shernand@iri.upc.edu)
3 // All rights reserved.
4 //
5 // This file is part of iriutils
6 // iriutils is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef _THREAD_H
20 #define _THREAD_H
21 
22 #include "mutex.h"
23 #include <string>
24 
25 #include <pthread.h>
26 
27 typedef enum{attached,starting,active,detached} thread_states;
28 
76 class CThread
77 {
78  private:
87  pthread_t thread;
88 
110  void *(*user_thread_function)(void *param);
111 
126  void *param;
127 
143  thread_states state;
144 
152  std::string thread_id;
153 
163  CMutex access;
164 
165  protected:
181  static void *thread_function(void *param);
182 
195  void set_id(const std::string& id);
196 
197  public:
223  CThread(const std::string& id);
224 
267  void attach(void *(*user_thread_function)(void *param),void *param);
268 
287  void start(void);
288 
307  void end(void);
308 
319  void kill(void);
320 
341  void detach(void);
342 
368  int get_state(void);
369 
386  std::string get_id(void);
387 
399  virtual ~CThread();
400 };
401 
402 #endif
static void * thread_function(void *param)
Internal thread function.
Definition: thread.cpp:166
Implementation of a parallel thread of execution.
Definition: thread.h:76
std::string get_id(void)
Function to get the thread identifier.
Definition: thread.cpp:161
Implementation of a mutual exclusion mechanism.
Definition: mutex.h:46
void attach(void *(*user_thread_function)(void *param), void *param)
Function to attach the user function of the thread.
Definition: thread.cpp:35
virtual ~CThread()
Destructor.
Definition: thread.cpp:177
int get_state(void)
Function to retrieve the state of the thread.
Definition: thread.cpp:139
void end(void)
Function to request the end of the execution of the thread.
Definition: thread.cpp:80
void detach(void)
Function to remove the assigned user function.
Definition: thread.cpp:126
void set_id(const std::string &id)
Function to set the thread identifier.
Definition: thread.cpp:150
void kill(void)
Function to immediatelly terminate the execution of the thread.
Definition: thread.cpp:100
void start(void)
Function to start the execution of the thread.
Definition: thread.cpp:53
CThread(const std::string &id)
Constructor.
Definition: thread.cpp:28