I am trying to create a base class with threads in C ++, but I get a seg error when I try to create a stream. Here is what GDB reports:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000401b68 in StartThread (pFunction=
0x401ad2 <FindPrimesThread(void*)>, pLimit=5000000) at Thread.cpp:35
35 state->mLimit = pLimit;
when I try to call it like this:
ThreadState *primesState = StartThread(FindPrimesThread, 5000000);
Here is my code:
Thread.hpp
#ifndef THREAD_HPP
#define THREAD_HPP
#include <pthread.h>
#include "Types.hpp"
typedef struct {
ulong mLimit;
int mStarted;
int mExitCode;
pthread_t mThreadId;
} ThreadState;
typedef void *(*ThreadFunction)(void *);
ThreadState *StartThread
(
ThreadFunction const pFunction,
ulong const pLimit
);
#endif
Thread.cpp
#include "Amicable.hpp"
#include "Keith.hpp"
#include "Main.hpp"
#include "Prime.hpp"
#include "Thread.hpp"
ThreadState *StartThread
(
ThreadFunction const pFunction,
ulong const pLimit
) {
ThreadState *state;
state->mLimit = pLimit;
pthread_t threadId;
state->mStarted = pthread_create(&threadId, NULL, pFunction, (void *)state);
if(state->mStarted == 0){
state->mThreadId = threadId;
}
return state;
}
Any idea what is going wrong here?
source
share