pthread problems

Hey everyone.

I am having a few problems with the pthread functions. I am trying to create a thread class with the following definition:
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
#ifndef CTHREAD_H_
#define CTHREAD_H_

#include <pthread.h>

class CThread
{
public:
	CThread();
	~CThread();

	//beginThreadExecution - start thread.
	void beginThreadExecution();

	//stopThreadExecution - stop thread.
	void stopThreadExecution();

	//threadEntryPoint - override this function for the thread task:
	virtual void threadEntryPoint();

protected:
	//m_stopRequested - see note above:
	volatile bool m_stopRequested;
private:
	volatile bool m_running;

    //start_threadStatic - Because pthread needs a static entry point,
//we define a static function here that will simply call the derived
	//static thread entry point:
    static void* start_threadStatic(void *obj);

	pthread_t* m_threadHandle;	//the thread handle.

};

#endif /* CTHREAD_H_ */ 


I keep however, getting a segfault with this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void CThread::beginThreadExecution()
{
	//start the thread:
	if(m_running == true)
	{
		//don't start twice:
		return;
	}

	//we are now starting the thread:
	m_running = true;
	pthread_create(m_threadHandle, 0,
			&CThread::start_threadStatic, this);
}


The problem is with the pthread_create function, I get the following output from GDB:

Program received signal SIGSEGV, Segmentation fault.
0x00f5dea0 in pthread_create@@GLIBC_2.1 () from /lib/tls/i686/cmov/libpthread.so.0

I've been scratching my head for days, does anyone have any insight?

Cheers,
Matt
Surely:
pthread_create(&m_threadHandle, 0, &CThread::start_threadStatic, this);

where threadHandle is:
pthread_t threadHandle;
Thanks for the quick reply, your post made me realise that I forgot to allocate memory for the pthread_t object. Just incase anyone is curious the solution was:
1
2
3
4
5
6
7
8
CThread::CThread()
{
	//set default vars:
	m_running = false;

	//create a new pthread object:
	m_threadHandle = (pthread_t*)malloc(sizeof(pthread_t));
}


Thanks,
Matt
You don't need to allocate it, you can simply declare it as I have done. It's clearer, simpler and contributes less to memory fragmentation.
I realise that by not malloc'ing it I don't have to worry about memory fragmentation, however, nearly every call within pthread requests a pointer to a pthread_t struct, therefore I find it much more clearer to just declare a pointer and then malloc that. Rather than using &m_threadHandle everywhere.
That really makes no sense to me. You are using dynamic memory when it is not needed. You are using malloc in a C++ program. You are not checking the return of malloc. You are not initializing the pointer in the member init list. You are not using an auto_ptr, which means you have to free() inside the destructor. Why submit yourself to such a huge bug farm when kbw's method is short, simple and works?
I have to agree that kbw has the better solution.

If your philosophy is to let what can be auto be auto then you are asking for fewer errors.

Topic archived. No new replies allowed.