A Thready Question
-
I've been recently experimenting with threads and found that I got an error when i tried to pass a class member function as the Thread entry point (something about not converting void(void *) to void(__cdcl)(void *). I tried attaching the __cdcl (somethin like __cdcl anyway) to the function definition but it did not help. Is this possible and if not is there a way to have global functions access member variables (friend keyword??)? - X
-
I've been recently experimenting with threads and found that I got an error when i tried to pass a class member function as the Thread entry point (something about not converting void(void *) to void(__cdcl)(void *). I tried attaching the __cdcl (somethin like __cdcl anyway) to the function definition but it did not help. Is this possible and if not is there a way to have global functions access member variables (friend keyword??)? - X
You cannot pass non-static functions as the thread proc. But what you can do is to pass a pointer to your class's instance as the LPVOID parameter Then you can cast the pointer to your class and use the members from the global function. Nish
-
You cannot pass non-static functions as the thread proc. But what you can do is to pass a pointer to your class's instance as the LPVOID parameter Then you can cast the pointer to your class and use the members from the global function. Nish
Thankyou greatly. A reply in 6 minutes. Who would have thought?? - X
-
Thankyou greatly. A reply in 6 minutes. Who would have thought?? - X
That's Code Project for you :-) Nish
-
I've been recently experimenting with threads and found that I got an error when i tried to pass a class member function as the Thread entry point (something about not converting void(void *) to void(__cdcl)(void *). I tried attaching the __cdcl (somethin like __cdcl anyway) to the function definition but it did not help. Is this possible and if not is there a way to have global functions access member variables (friend keyword??)? - X
See the VC forum FAQ. :) --Mike-- http://home.inreach.com/mdunn/ If there's something strange / in your VC code / Who you gonna call? / Ghostbusters! :love: your :bob: with :vegemite: and :beer:
-
I've been recently experimenting with threads and found that I got an error when i tried to pass a class member function as the Thread entry point (something about not converting void(void *) to void(__cdcl)(void *). I tried attaching the __cdcl (somethin like __cdcl anyway) to the function definition but it did not help. Is this possible and if not is there a way to have global functions access member variables (friend keyword??)? - X
You can make the function as Static member of the class(instead of global class) and then pass it to thread function. Deepak Khajuria
-
You can make the function as Static member of the class(instead of global class) and then pass it to thread function. Deepak Khajuria
But then he wont be able to access the non-static members Nish
-
But then he wont be able to access the non-static members Nish
Yes, he has to pass the instance of the class as parameter,or something similar. But the main difference is that the static function of the class has full access to the class, where as global function cannot access the protected/private members. Deepak Khajuria
-
Yes, he has to pass the instance of the class as parameter,or something similar. But the main difference is that the static function of the class has full access to the class, where as global function cannot access the protected/private members. Deepak Khajuria
He can always call the static functions of his class from his global function anyway. Thus he can also access all static members (via the static function, of course) Nish
-
He can always call the static functions of his class from his global function anyway. Thus he can also access all static members (via the static function, of course) Nish
The static functions & global functions are same in sense that you do have instance of the class(this) in it. You need to get instance of the class from somewhere to call its non-static members. But the main difference is that the static function of the class has access to the protected/private members of the object of same class. This does not hold true for the global functions.(unless you make them friend) So in a thread where you require direct access to class protected/private members, it is better to make thread function as static member of the class. Deepak Khajuria