| | 166 | |
| | 167 | === Wrapping Linux Threads in C++ classes === |
| | 168 | |
| | 169 | Refer to Root software: |
| | 170 | |
| | 171 | esting the root TThread class, we made some experiences |
| | 172 | running a class member function as a (Linux-)pthread, which might help you. |
| | 173 | |
| | 174 | Since pthread_create (and also the TThread constructor) expects a C function to be run, |
| | 175 | it needs the pointer to the class instance as function argument (4th argument of |
| | 176 | pthread_create), if you want to run a member function of this C++ class. |
| | 177 | When C++ methods are called from within a class, internally the class instance pointer |
| | 178 | ("this") is always passed as invisible first argument. |
| | 179 | Thus the "C" call of any C++ member function by pointer requires |
| | 180 | the class object address as first argument... |
| | 181 | |
| | 182 | For example, |
| | 183 | in root TThread class, the implementation for pthreads (THREAD_PosixThread.cxx) |
| | 184 | starts the member function void* TThread::Fun (which calls your own function |
| | 185 | passed to the TThread constructor) as pthread with the pointer to the TThread instance |
| | 186 | TThread* th as argument: |
| | 187 | {{{ |
| | 188 | int ierr = pthread_create(&id, attr, &TThread::Fun,th); |
| | 189 | }}} |
| | 190 | (in method TPosixThread::Run(TThread* th)) |
| | 191 | |
| | 192 | Additionally, if you want to run a member void myfunction of any class Myclass as TThread, |
| | 193 | you also have to pass the address to the class as function argument of TThread constructor, |
| | 194 | since TThread::Fun uses a C function call to invoke your function: |
| | 195 | {{{ |
| | 196 | TThread* mythread= new TThread("My Thread", |
| | 197 | (void(*) (void *)) &Myclass::myfunction, |
| | 198 | (void*) &Myclass); |
| | 199 | mythread->Run(); |
| | 200 | }}} |
| | 201 | when you create TThread from within class Myclass, you may write |
| | 202 | {{{ |
| | 203 | TThread* mythread= new TThread("My Thread", |
| | 204 | (void(*) (void *)) &myfunction, |
| | 205 | (void*) this); |
| | 206 | mythread->Run(); |
| | 207 | }}} |
| | 208 | The required cast of &myfunction produced a compiler warning in our tests |
| | 209 | (egcs-2.91.66 Debian GNU/Linux (egcs-1.1.2 release)); however, the example |
| | 210 | was compiled and runs. |
| | 211 | |
| | 212 | And |
| | 213 | please don't mix a ptr-to-function and a ptr-to-member-function. |
| | 214 | The former is just the address of a function. The later is |
| | 215 | a structure containing |
| | 216 | |
| | 217 | * the function address for non-virtual functions |
| | 218 | * the vtable index for virtual functions |
| | 219 | * the this pointer offset |
| | 220 | |
| | 221 | The representation of the structure is implementation specific, |
| | 222 | and does for some compilers even depend on whether the class |
| | 223 | has virtual functions or multiple inheritance or not. |
| | 224 | |
| | 225 | All this is needed to calculate |
| | 226 | * the actual entry point address |
| | 227 | * and the effective this pointer |
| | 228 | when the ptr-to-member-function is finally used in a call. |
| | 229 | In cases like multiple inherited base classes with virtual |
| | 230 | functions in virtual base classes things get nontrivial here! |
| | 231 | |
| | 232 | Inspect the C++ lanuage definition for the allowed conversions |
| | 233 | and casts of a ptr-to-member-function, and you'll see, there are |
| | 234 | very few. |
| | 235 | |
| | 236 | In the light of all this I wonder portable Joern's code example is. |
| | 237 | |
| | 238 | Beyond that, the code |
| | 239 | {{{ |
| | 240 | TThread* mythread= new TThread("My Thread", |
| | 241 | (void(*) (void *)) &Myclass::myfunction, |
| | 242 | (void*) &Myclass); |
| | 243 | }}} |
| | 244 | will probably not give the desired result if Myclass uses |
| | 245 | multiple inheritance and myfunction is from one of the base |
| | 246 | classes (that's why the PTMF struct may contain a this offset). |
| | 247 | |
| | 248 | And |
| | 249 | |
| | 250 | Pthread standard allows only simple C function. |
| | 251 | |
| | 252 | TThread::Fun is static member of a class. A static member function == C function. |
| | 253 | So it will work with all compilers/platforms. |
| | 254 | I introduced static member function TThread::Fun and not simple C function |
| | 255 | only do not pollute the name space. |
| | 256 | |
| | 257 | But you still can use nonstatic virtual member function in Pthread. |
| | 258 | |
| | 259 | For example you want to call virtual finction void A::F() |
| | 260 | By C++ rules you mas provide 2 pointers: |
| | 261 | A* p - pointer to class instance |
| | 262 | void (A::*f)() - pointer to function |
| | 263 | |
| | 264 | Then you calculate pointer f = &A::F; |
| | 265 | |
| | 266 | User function for Pthread |
| | 267 | {{{ |
| | 268 | user(void* adr) |
| | 269 | { |
| | 270 | A inst; |
| | 271 | (A::*f)() = ((A::*)())adr; |
| | 272 | (inst.*f)(); |
| | 273 | } |
| | 274 | }}} |
| | 275 | In the example instance of A:: created inside of function. |
| | 276 | But it is easy to pass this pointer via intermediate object |
| | 277 | which keeps both poiners, to instance and to function. |
| | 278 | Pointer to this object passed by adr uf user routine. |
| | 279 | |
| | 280 | So, there is no any limitation in THread:: |
| | 281 | |
| | 282 | |
| | 283 | |
| | 284 | |