I am attempting to create my own Thread class in C++ that resembles the Java Thread object. I understand that C++ does not use implementation so instead I am keeping a reference to a function as a variable in my C++ Thread Object.
I am having trouble with the second constructor of my Thread Object where you as the user of my thread object are to specify your own function that you want to run.
I am getting a message that says
Thread.cpp:23:58: error: invalid conversion from ‘void ()(void)’ to ‘void* ()(void)’ [-fpermissive]
#ifndef THREAD_H
#define THREAD_H
#include <iostream>
#include <pthread.h>
#include <cstdlib>
#include <string.h>
class Thread
{
public:
Thread();
Thread(void (*f)(void*));
~Thread();
void* run(void*);
void start();
private:
pthread_t attributes;
int id;
void (*runfunction)(void*); //Remember the pointer to the function
};
void* runthread(void* arg); //header
#endif
And here is my C++ file
#include "Thread.h"
Thread::Thread()
{
memset(&attributes,0,sizeof(attributes));
}
Thread::Thread(void (*f)(void*))
{
runfunction = f;
//(*f)();
}
Thread::~Thread()
{
id = pthread_create(&attributes, NULL, runthread,this);
}
void Thread::start()
{
memset(&attributes,0,sizeof(attributes));
id = pthread_create(&attributes, NULL, *runfunction,this);
}
void* Thread::run(void*)
{
}
void* runthread(void* arg)
{
Thread* t = (Thread*) arg;
t->run(NULL);
}