_beginthread, can we create a thread in a class? [modified]
-
Hello, I have a class which i created, it calculates some mathematical functions, Can i use or create "_beginthread" in the class? i tried to do it, but i got this error:
error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)'
class CMaClasse { public: void FoncAppnt(void* args); void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... }
-- modified at 18:18 Friday 9th November, 2007 -
Hello, I have a class which i created, it calculates some mathematical functions, Can i use or create "_beginthread" in the class? i tried to do it, but i got this error:
error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)'
class CMaClasse { public: void FoncAppnt(void* args); void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... }
-- modified at 18:18 Friday 9th November, 2007Gofur Halmurat wrote:
Can i use or create "_beginthread" in the class?
Sure. But, if your thread proc is a class method, that method needs to be static...
class CMaClasse { public: void FoncAppnt(void* args); static void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(&``CMaClasse::``FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... }
MarkMark Salsbery Microsoft MVP - Visual C++ :java:
-
Gofur Halmurat wrote:
Can i use or create "_beginthread" in the class?
Sure. But, if your thread proc is a class method, that method needs to be static...
class CMaClasse { public: void FoncAppnt(void* args); static void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(&``CMaClasse::``FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... }
MarkMark Salsbery Microsoft MVP - Visual C++ :java:
Can I use public variable in static function? because i have some public variables in the class
-
Can I use public variable in static function? because i have some public variables in the class
A static method can only access static class members. To get around this, typically a pointer to an object of the class is passed to the thread proc. Then the thread proc can use that pointer to access non static members of the class. Here's an example (my changes are in red)...
class CMaClasse
{
void *pThreadArgs;
public:
void FoncAppnt(void* args);
static void FoncApp(void* arg);
};void CMaClasse::FoncAppnt(void* args)
{
pThreadArgs = args;_beginthread(&CMaClasse::FoncApp,0,this);
}void CMaClasse::FoncApp(void* arg)
{
// The passed param is a CMacClasse pointer
CMaClasse *pThis = (CMaClasse *)arg;// example of accessing a class member
pThis->pThreadArgs->...//...
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
A static method can only access static class members. To get around this, typically a pointer to an object of the class is passed to the thread proc. Then the thread proc can use that pointer to access non static members of the class. Here's an example (my changes are in red)...
class CMaClasse
{
void *pThreadArgs;
public:
void FoncAppnt(void* args);
static void FoncApp(void* arg);
};void CMaClasse::FoncAppnt(void* args)
{
pThreadArgs = args;_beginthread(&CMaClasse::FoncApp,0,this);
}void CMaClasse::FoncApp(void* arg)
{
// The passed param is a CMacClasse pointer
CMaClasse *pThis = (CMaClasse *)arg;// example of accessing a class member
pThis->pThreadArgs->...//...
}Mark Salsbery Microsoft MVP - Visual C++ :java:
thanks Mark, It helped me alot!!!
-
Hello, I have a class which i created, it calculates some mathematical functions, Can i use or create "_beginthread" in the class? i tried to do it, but i got this error:
error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)'
class CMaClasse { public: void FoncAppnt(void* args); void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... }
-- modified at 18:18 Friday 9th November, 2007Here's a tutorial that may be helpful. I tried to post this earlier but got some goofy error. http://www.computersciencelab.com/MultithreadingTut1.htm[^]
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
Hello, I have a class which i created, it calculates some mathematical functions, Can i use or create "_beginthread" in the class? i tried to do it, but i got this error:
error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)'
class CMaClasse { public: void FoncAppnt(void* args); void FoncApp(void* arg); }; void CMaClasse::FoncAppnt(void* args) { _beginthread(FoncApp,0,(void*)a); } void CMaClasse::FoncApp(void* arg) { //... }
-- modified at 18:18 Friday 9th November, 2007Just to explain this behavior see: http://www.parashift.com/c++-faq-lite/pointers-to-members.html. Briefly, the type of pointer-to-member-function is different from pointer-to-function .There is not cast (new in VC++2005).
-
Just to explain this behavior see: http://www.parashift.com/c++-faq-lite/pointers-to-members.html. Briefly, the type of pointer-to-member-function is different from pointer-to-function .There is not cast (new in VC++2005).
george.dumitru wrote:
There is not cast (new in VC++2005).
I never heard of "not cast"! :confused:
"We make a living by what we get, we make a life by what we give." --Winston Churchill
-
george.dumitru wrote:
There is not cast (new in VC++2005).
I never heard of "not cast"! :confused:
"We make a living by what we get, we make a life by what we give." --Winston Churchill