GTK+ IOStream  Beta
<< GTK+ >> add C++ IOStream operators to GTK+. Now with extra abilities ... like network serialisation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Thread.H
Go to the documentation of this file.
1 /* Copyright 2000-2013 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef THREAD_H_
18 #define THREAD_H_
19 
20 #ifdef USE_USE_GLIB_THREADS
21 #include <glib.h>
22 #else
23 #include <pthread.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #endif
27 
28 #include <iostream>
29 using namespace std;
30 
33 class Thread {
34 
35 #ifdef USE_GLIB_THREADS
36  GThread* thread;
37  GError *threadErrorPtr;
38 #else
39  pthread_t thread;
40 #endif
41 public:
44  Thread(void) {
45 #ifdef USE_GLIB_THREADS
46  thread=NULL;
47  threadErrorPtr=NULL;
48 #endif
49  }
50 
53  virtual ~Thread(void) {
54 #ifdef USE_GLIB_THREADS
55  g_thread_join(thread);
56  thread=NULL;
57 #else
58 // void *retVal;
59  pthread_cancel(thread); // this returns error of ESRCH if the thread is already finished
60 
61 // int threadResp=pthread_join(thread, &retVal);
62  // on destruction, not interested in the return value here, just want to make sure the thread has exited.
63 // if (threadResp!=0) {
64 // errno=threadResp;
65 // perror("Thread::meetThread : Couldn't meet the thread");
66 // cerr<<"Most likely that the thread isn't running."<<endl;
67 // }
68 #endif
69  }
70 
76 #ifdef USE_GLIB_THREADS
77  int run(void (*start_routine) (void *), void *data) {
78 #else
79  int run(void *(*start_routine) (void *), void *data) {
80 #endif
81 #ifdef USE_GLIB_THREADS
82  thread = g_thread_create((GThreadFunc)start_routine, (gpointer) data, TRUE, &threadErrorPtr);
83  // TODO : check the response of g_thread_create on error
84  return (int)thread;
85 #else
86  pthread_attr_t attr;
87  pthread_attr_init(&attr);
88  pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
89 // int pthread_setcanceltype(int type, int *oldtype);
90 
91  int threadResp=pthread_create(&thread, &attr, start_routine, data);
92  if (threadResp!=0) {
93  errno=threadResp;
94  perror("Thread::run : Thread creation failed.");
95  }
96  pthread_attr_destroy(&attr);
97  return threadResp;
98 #endif
99  }
100 
104  void *meetThread(void) {
105 #ifdef USE_GLIB_THREADS
106  g_thread_join(thread);
107  thread=NULL;
108  return NULL;
109 #else
110  void *retVal;
111  int threadResp=pthread_join(thread, &retVal);
112  if (threadResp!=0) {
113  errno=threadResp;
114  perror("Thread::meetThread : Couldn't meet the thread");
115  }
116  return retVal;
117 #endif
118  }
119 
123  void exit(void *retVal) {
124 #ifndef USE_GLIB_THREADS
125  pthread_exit(retVal);
126 #endif
127  }
128 };
129 #endif // THREAD_H_