Creating a thread from an Member Function?

Jun 9, 2009 at 12:54pm
Hey I want to create a thread by using an member function as the threads function?
But somehow im too dumb... is this even possible with class member functions?

1
2
3
4
5
void MyClass::Accept()
{
	...
	_beginthread(MyClass::DDX,NULL,NULL);
}


The Compiler says, that its missing the argument list...(DDX);
Last edited on Jun 9, 2009 at 12:54pm
Jun 9, 2009 at 1:45pm
You forgot a &. It's &MyClass::DDX. Although there are other problems:

But somehow im too dumb... is this even possible with class member functions?


Not unless the function is static. The thing is, non-static member functions require an additional, hidden "this" parameter. So you cannot give pointers to member functions for global function pointers (which is what threading libs and the like usually take)

Although you can have a static member call a non-static member if you provide a pointer to an object. I'm not familiar with _beginthread, but one of those arguments you're passing NULL to is probably a "userdata" argument. Instead of passing NULL, pass a pointer to your object (ie: "this"). Here's an example of how you'd get this working:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// assuming the thread callback looks like:
//   int ThreadCallback(void* userdata)

class MyClass
{
//...
    static int StaticThreadEntry(void* userdata)
    {
        MyClass* p = static_cast<MyClass*>(userdata);
        return p->ThreadEntry();
    }

    int ThreadEntry()
    {
        // thread enters here
        
        return 0;
    }


    void Accept()
    {
        //...
        _beginthread(&MyClass::StaticThreadEntry,this,NULL);
    }
//...
};
Jun 9, 2009 at 5:56pm
the funny thing is: i dont want to pass any arguments to the thread... but i need the static function to interopertate with the non-static members of the object created, which is not allowed-.-'...

what i want to is a sperate thread for any client connection through winsock...

refering to the above example, am i able to call this->ThreadEntry?... or shall i create an static class with everything static to interop?:D...

So you cannot give pointers to member functions for global function pointers
<- i dont get this
Last edited on Jun 9, 2009 at 6:02pm
Jun 9, 2009 at 6:00pm
i dont want to pass any arguments to the thread

Why not?
Jun 9, 2009 at 6:14pm
i am still writing my chat application ( slow progress - i dont do it every week);

and the thread i want to create is a thread that receives the data from the client and logs it into a file... there for i want to use an mutex, which controls the data input into the file and that every thread has access to...

oh right... i want to revoke my statement, that i dont want to pass anything... i will pass the client socket^^... my bad...

but the problem is, that i only want to create one Server object which holds all the functions... so the mutex is an member of it... purely hypothetical: i don´t want to create it as static...
Jun 9, 2009 at 6:20pm
i will pass the client socket

Which is presumably stored in the client object. So pass the object pointer.
Last edited on Jun 9, 2009 at 6:20pm
Jun 9, 2009 at 7:08pm
am i able to access any of my object members from the static function through a pointer - for expample the mutex?...
Jun 10, 2009 at 12:46am
the funny thing is: i dont want to pass any arguments to the thread... but i need the static function to interopertate with the non-static members of the object created, which is not allowed-.-'...


This is exactly what my example illustrated. Did you try it?

The static funciton calls a nonstatic function by using the 'this' pointer supplied by the userdata parameter. Since you ultimately reach a non-static function, you now have an object instance whose members you can access.

refering to the above example, am i able to call this->ThreadEntry?


Provided you have a this pointer, yes. But since ThreadEntry is not static, you can't pass it as the entry point to _beginthread. This is why you need to use the static funciton.

... or shall i create an static class with everything static to interop?:D...


You could do this, but it would probably not be what you want.

am i able to access any of my object members from the static function through a pointer


Provided you have a valid pointer, yes. But it's easier to just call a member function with that pointer like in my example, that way you don't have to keep typing "myobj->Whatever()" for everything.
Jun 10, 2009 at 1:02am
Hold on. This is _beginthread()'s prototype:
1
2
3
4
5
uintptr_t _beginthread( 
   void( *start_address )( void * ),
   unsigned stack_size,
   void *arglist 
);

You can't pass a method because pointers to methods have a different type.
What I do when I need to start a thread and I want it to remain method-esque is this:
1
2
3
4
5
6
7
void thread(void *param){
	A::staticThreadFunction((A *)param);
}

void A::staticThreadFunction(A *_this){
	//...
}
Jun 10, 2009 at 3:03am
This works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <process.h> // _beginthread()

class A {
public:
    static void threadFunc( void* param );
    void func() { std::cout << "Member func\n"; }
};

void A::threadFunc( void* a ){
    std::cout << "In threadFunc\n";
    ((A*)a)->func();
}

int main(){
    A a;
    _beginthread( A::threadFunc, 0, &a );
std::cin.get();
}

Jun 10, 2009 at 1:26pm
wouldnt that cause a problem, while many threads calling a->func() simoultaniously?...
Jun 10, 2009 at 1:32pm
Only if the function is non-reentrant.
Jun 10, 2009 at 2:25pm
for my understanding... so there will be this one function running multiple times concurrently.Or scheduled one request after another?... (sorry fpr those basic-questions)


to expand this: so would it be able to compute many incoming chats from many different sockets in every threadf by calling this function which gets the socket as an argument and still can access the class objects´ ressources?
Last edited on Jun 10, 2009 at 2:27pm
Jun 10, 2009 at 2:44pm
Threads run concurrently, obviously. "Reentrant" meant that the function can safely run both concurrently and recursively. For the former, this is done by not allowing it to write shared data (such as members or global variables) without using mutexes. If the function only reads, there's no problem (usually). For the latter, this is done by avoiding writing shared data (particularly static local data) altogether.
Jun 10, 2009 at 3:31pm
na, i meant the func()-Function the ThreadFunctions calls...
Topic archived. No new replies allowed.