1

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);
}
3
  • 4
    not using C++11 threading capabilities is a deliberate design decision? If not, please take a look at it, because I feel like you are reinventing the wheel. Commented Aug 28, 2014 at 14:49
  • "C++ does not use implementation". Actually it does, just not the keyword. Inheritance in C++ is more flexible than in Java so "implements" happens by design rather than as specific a language feature. Commented Aug 28, 2014 at 15:26
  • 1
    I think my appreciation for Java and C++11 is higher when I have worked on understanding what was required to make our lives easier for higher level programmers... Commented Aug 28, 2014 at 18:00

1 Answer 1

2

pthread_create expects a function with one void* parameter returning void*, and you provide a function returning void.

But, as comments say, use C++ builtin threading is better option.

Sign up to request clarification or add additional context in comments.

8 Comments

Do you have suggestions for my code? I don't understand how to fix it. I am trying to combine functions as arguments with threads. As far as not using the built in one, I haven't explored that yet and want to be comfortable how old school threads worked first.
@Matthew: As this answer and the error message both say: to fix it, change the type of the function pointers (runfunction, and the constructor argument f) to return void* not void, since that's what pthread_create expects.
When I do that, I can not longer pass in a function as an argument though. stackoverflow.com/questions/9410/…
If you pass a function returning void* it should work.
@Matthew: The function will also have to return void*. Just return NULL if you don't want to pass any information back to the thread that joins it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.