threads.cpp
Go to the documentation of this file.
1 
2 #include "threads.h"
3 
4 
5 void Thread::start() {
6  int r;
7  //this->arg = arg;
8  if ((r = pthread_create(&_id, NULL, &Thread::exec, this)) != 0) {
9  cout << strerror(r) << endl;
10  throw "Error";
11  }
12 }
13 
14 
15 
16 void Thread::join() {
18  // Allow thread to wait for the termination status
20  void *status;
21  int r = pthread_join(_id, &status);
22  if (r) {
23  printf("ERROR in thread %d; return code from pthread_join() is %d\n",my_id, r);
24  getchar(); getchar(); getchar(); // dsy
25  exit(-1);
26  }
27  //if (DEBUG) printf("Joined with thread %ld, status of %ld\n", my_id, (long) status);
28 }
29 
30 
31 
32 void *Thread::exec(void *thr) {
34  // Function that is to be executed by the thread
36  reinterpret_cast<Thread *>(thr)->run();
37  // ((Thread *)thr)->run(); ?? are these the same
38  return NULL;
39 }
40 
41 
42 
43 /*
44 void Thread::start(void *arg) {
45  int ret;
46  this->arg = arg;
47  *
48  * Since pthread_create is a C library function, the 3rd argument is
49  * a global function that will be executed by the thread. In C++, we
50  * emulate the global function using the static member function that
51  * is called exec. The 4th argument is the actual argument passed to
52  * the function exec. Here we use this pointer, which is an instance
53  * of the Thread class.
54  *
55  if ((ret = pthread_create(&_id, NULL, &Thread::exec, this)) != 0) {
56  cout << strerror(ret) << endl;
57  throw "Error";
58  }
59 } */
virtual void run()=0
pthread_t _id
Definition: threads.h:24
void start()
Definition: threads.cpp:5
int my_id
Definition: threads.h:67
void join()
Definition: threads.cpp:16
Definition: threads.h:20
static void * exec(void *thr)
Definition: threads.cpp:32


co_scan
Author(s):
autogenerated on Mon Feb 28 2022 23:00:54