cheshirekow  v0.1.0
thread.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of cpp-pthreads.
5  *
6  * cpp-pthreads is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU 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  * cpp-pthreads 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 General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with cpp-pthreads. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef CPP_PTHREADS_THREAD_H_
28 #define CPP_PTHREADS_THREAD_H_
29 
30 #include <pthread.h>
31 #include <cpp_pthreads/attr.h>
32 
33 namespace pthreads {
34 
37 template<class T>
38 void* callCallable(void* obj) {
39  (*static_cast<T*>(obj))();
40  return obj;
41 }
42 
44 class Thread {
45  public:
47  typedef void *(*routine_t)(void*);
48 
49  private:
50  pthread_t m_data;
51 
52  public:
54  pthread_t c_obj() {
55  return m_data;
56  }
57 
59  int launch(routine_t start, void* arg = 0);
60 
62  int launch(const Attr<Thread>& attr, routine_t start, void* arg = 0);
63 
65  template<typename Callable>
66  int launch(Callable* obj) {
67  return launch(callCallable<Callable>, obj);
68  }
69 
72  template<typename Callable>
73  int launch(const Attr<Thread>& attr, Callable* obj) {
74  return launch(attr, callCallable<Callable>, obj);
75  }
76 
78  int join(void** value_ptr = 0);
79 
81  int detach();
82 
84  int cancel();
85 
87  int kill(int sig);
88 
90  bool operator==(const Thread& other);
91 
93  static void exit(void* value_ptr);
94 
96  static Thread self();
97 };
98 
99 } // namespace pthreads
100 
101 #endif // THREAD_H_
bool operator==(const Thread &other)
calls pthread_equal
int cancel()
cancel this thread
int detach()
detach a thread, the thread will be destroyed when it exits
int launch(routine_t start, void *arg=0)
start a new thread storing the id in m_data
A thread... presumably you know what this is.
Definition: thread.h:44
int launch(Callable *obj)
start a new thread which calls the callable object
Definition: thread.h:66
pthread_t m_data
Definition: thread.h:50
int join(void **value_ptr=0)
join a thread, calling thread blocks until this thread has exited
void *(* routine_t)(void *)
type of a function which the thread will invoke
Definition: thread.h:47
BinaryKey other(const BinaryKey &key)
Definition: BinaryKey.h:44
void * callCallable(void *obj)
function which invokes a callable object when that object is passed to launch a thread ...
Definition: thread.h:38
Attributes object for type Base.
Definition: attr.h:35
int kill(int sig)
send a signal to the thread
pthread_t c_obj()
return underlying object
Definition: thread.h:54
static void exit(void *value_ptr)
terminates the calling thread
int launch(const Attr< Thread > &attr, Callable *obj)
start a new thread which calls the collable object and has the specified attributes ...
Definition: thread.h:73