Simple question
Hello, i'm developing simple wrapper classes for posix threads and mutexes. Here are the sources
when compiling these sources i'm getting this error:
/var/tmp//cc1V2mOd.o: In function `LThread::LThread[in-charge](thread*)':
/var/tmp//cc1V2mOd.o(.gnu.linkonce.t._ZN 7LThreadC1EP6thread+0xa): undefined reference to `thread_body(void*)'
collect2: ld returned 1 exit status
compiler is GCC 3.2
any ideas why it is so?
P.S. I understand that there are a lot of implementations for thread/mutexes classes, but i want this one to become working :)
MutexInterface.h
#ifndef MUTEXINTERFACE_H
#define MUTEXINTERFACE_H
class MutexInterface
{
public:
virtual void enterMutex() = 0;
virtual int tryEnterMutex() = 0;
virtual void leaveMutex() = 0;
};
#endif
PosixMutex.h
#ifndef POSIX_MUTEX_H
#define POSIX_MUTEX_H
#include "MutexInterface.h"
#include <pthread.h>
class PosixMutex : public MutexInterface
{
private:
pthread_mutex_t *mutex;
public:
PosixMutex()
{
mutex = new pthread_mutex_t;
}
virtual ~PosixMutex()
{
delete mutex;
}
virtual void enterMutex()
{
pthread_mutex_lock(mutex);
}
virtual int tryEnterMutex()
{
return pthread_mutex_trylock(mutex);
}
virtual void leaveMutex()
{
pthread_mutex_unlock(mutex);
}
};
#endif
ThreadInterface.h
#ifndef THREAD_INTERFACE_H
#define THREAD_INTERFACE_H
class ThreadInterface
{
public:
virtual void start() = 0;
};
#endif
<b>PosixThread.h</b>
#ifndef POSIX_THREAD_H
#define POSIX_THREAD_H
#include "ThreadInterface.h"
class PosixThread : public ThreadInterface
{
private:
pthread_t *cp_thread;
void *p_param;
void *(*fp)(void*);
public:
PosixThread(void* lp_param, void *(*routine)(void*))
{
cp_thread = new pthread_t;
p_param = lp_param;
fp = routine;
}
virtual ~PosixThread()
{
delete cp_thread;
}
virtual void start()
{
pthread_create(cp_thread,0,fp, (void*)(p_param));
}
};
#endif
main.cpp
#include <iostream>
#include <unistd.h>
#include "PosixMutex.h"
#include "PosixThread.h"
PosixMutex mutex;
struct thread {
int threadID;
};
void *thread_body(void *params);
class LThread : public PosixThread
{
public:
LThread(struct thread* ptrData) : PosixThread((void*) ptrData, (void*(*)(void*)) ::thread_body) {}
void *thread_body(void *params);
int printThreadAndSleep(thread *data);
private:
};
int LThread::printThreadAndSleep(thread *data) {
if (!mutex.tryEnterMutex())
{
std::cerr << "Succesfully locked by " << data->threadID << std::endl;
} else {
std::cerr << "Failed to lock - waiting (" << data->threadID << ")" << std::endl;
mutex.enterMutex();
}
sleep(5);
std::cerr << "Released by (" << data->threadID << ")" << std::endl;
mutex.leaveMutex();
}
void *LThread::thread_body(void *params)
{
thread *data = (thread*) params;
while (1)
{
std::cerr << data->threadID << std::endl;
printThreadAndSleep(data);
sleep(2);
}
}
int main()
{
pthread_t *cp_thread = new pthread_t;
struct thread *data;
for (int i=1;i<6;i++)
{
data = new thread;
data->threadID=i;
LThread *p=new LThread(data);
p->start();
}
sleep(100);
}
when compiling these sources i'm getting this error:
/var/tmp//cc1V2mOd.o: In function `LThread::LThread[in-charge](thread*)':
/var/tmp//cc1V2mOd.o(.gnu.linkonce.t._ZN
collect2: ld returned 1 exit status
compiler is GCC 3.2
any ideas why it is so?
P.S. I understand that there are a lot of implementations for thread/mutexes classes, but i want this one to become working :)
