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?


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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#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
Topic archived. No new replies allowed.