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);
}
Matthew
  • 3,886
  • 7
  • 47
  • 84
  • 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. – MariusSiuram Aug 28 '14 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. – Galik Aug 28 '14 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... – Matthew Aug 28 '14 at 18:00

1 Answers1

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.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • 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 Aug 28 '14 at 15:07
  • @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. – Mike Seymour Aug 28 '14 at 15:14
  • When I do that, I can not longer pass in a function as an argument though. http://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c – Matthew Aug 28 '14 at 15:18
  • 1
    If you pass a function returning `void*` it should work. – Wojtek Surowka Aug 28 '14 at 15:21
  • @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. – Mike Seymour Aug 28 '14 at 15:25
  • Awesome! Sorry about that! Thanks! So now the "user" of my Thread object must provide a void* function(void* x) Looks kind of confusing coming from Java. thank you! – Matthew Aug 28 '14 at 15:25
  • @Matthew: You'll find the C++ threading library less confusing. The `void*` malarkey is because pthreads is a C API, and that's the only sensible way in C to pass generic data in and out of the thread. You could get your class to deal with that (as `std::thread` does), so the user doesn't have to deal with `void*`. – Mike Seymour Aug 29 '14 at 13:49
  • @Mike Seymour, thanks for the information! My goal in this experiment is design a web document that shows how easy it is to get a chat program (server/client) up in Java using Java's JFrame, ServerSocket, and Thread objects. I then want to show how to do the same thing in generic linux. I am running into the problem that GTK is based in C so if I want to create GUIs, I have to stick to C. I don't know if it is worth it to continue down that road or not. – Matthew Aug 29 '14 at 17:12