thread wrapper class

Hi all,
I'm writing a wrapper thread class.
Because Posix "pthread_create" needs a static function, the concrete classes that going to use
the "start" methods will always find a bottleneck in the static method "run_thread"?
I saw different post where some people suggests extern C global function.
Is this the solution? But it must be synchronized?

#include <pthread.h>

class CCThread
{

private:

pthread_mutex_t ccmutex;

/**
This static method must be synchronized with static lock but how I said before this make a bottleneck!!
**/
static void* run_thread(void* ptr)
{
CCThread* thread = (CCThread*)ptr;
thread->run();
}

protected:
public:

CCThread()
{
pthread_mutex_init(&ccmutex,NULL);
}

virtual void run()=0;

pthread_t start()
{
pthread_t thread_id;

//to synchronize start method I don't know how the concrete classes will call
// this method. Ex. Many threads will call this method on the same instance.
pthread_mutex_lock(&ccmutex);

pthread_create(&thread_id,0x0,&CCThread::run_thread,this);
pthread_mutex_unlock(&ccmutex);

return thread_id;
}


~CCThread()
{
pthread_mutex_destroy(&ccmutex);
}

};


thanks to all
hottosho
Last edited on
closed account (o3hC5Di1)
Hi there,

First, please use code tags when posting: [code]code here[/code], it makes it a lot more readable to us.

Second, I'm afraid your chances of getting a reply will be better in the "general c++ programming" forum, as this is not exactly beginner's material.

Hope that helps.

All the best,
NwN

I'm working in the same thing just for learn.

I have made a base class using an extern C global function and with a virtual function that a derived class rewrites.

All works fine the first time, but the second time I call a method from the derived class, it runs a method from the base class and I don't know why.

If you are still interested we could work together.

Best regards Germinx.
Last edited on
I'm writing a wrapper thread class.
Yay!

Because Posix "pthread_create" needs a static function, the concrete classes that going to use the "start" methods will always find a bottleneck in the static method "run_thread"?
Why do you think it's a bottleneck? It's just a non-threaded function that is used in the creation of your thread. There's no bottleneck there.

I saw different post where some people suggests extern C global function.
No, don't do that. What you have is pretty standard. As far as I can tell, it was first coined by Pete Becker 'back in 94. It may have been published in the C++ Report back then. It became the model for Java threads.

Is this the solution? But it must be synchronized?
Seems ok to me.

You may find this thread helpful.
http://www.cplusplus.com/forum/general/27758/
Topic archived. No new replies allowed.