2

First of all, what is the difference between thread and pthread. What should I use with in C++.

I am trying with pthread as follows:

//MyPatcher.h

class MyPatcher
{
   public:
     void createThread();

   private:
     void * Checkingdata(void *arg);
}

// MyPatcher.cpp
#include "MyPatcher.h"
#include <pthread.h>
using namespace std;

void MyPatcher::createThread()
{
  pthread_t threadDataReading;

  if(pthread_create(&threadDataReading, NULL, Checkingdata, NULL))  
    printf("Could not create thread\n");

  if(pthread_join(threadReadingGps, NULL))  
    printf("Could not join thread\n");
}

void * MyPatcher::Checkingdata(void *arg)
{
  return NULL;
}

but I run to these problems:

./MyPatcher.cpp:71:73: error: argument of type 'void* (SampleApp::MyPatcher::)(void*)' does not match 'void* (*)(void*)'

How can I solve this problem?

// I then try with thread as well:

//MyPatcher.h

    class MyPatcher
    {
       public:
         void createThread();

       private:
         void Checkingdata();
    }

    // MyPatcher.cpp
    #include "MyPatcher.h"
    #include <thread>
    using namespace std;

    void MyPatcher::createThread()
    {
      pthread_t threadDataReading(Checkingdata);

      threadDataReading.join();
    }

    void MyPatcher::Checkingdata()
    {

    }

but I got this problem: error: no matching function for call to 'std::thread::thread()'

olidev
  • 20,058
  • 51
  • 133
  • 197
  • 2
    `pthread` stands for posix thread. thread is a concept, pthread is an implementation. – UmNyobe Feb 21 '13 at 13:19
  • 1
    @UmNyobe thread (`std::thread`) is also a class in C++ now. – juanchopanza Feb 21 '13 at 13:21
  • @juanchopanza the `std::thread` name only means that it handles threads. if it uses pthreads or any other mean is an implementation detail. – Javier Feb 21 '13 at 13:26
  • @Javier I know. But now when people say "thread" in a C++ context, they could mean either the concept, or the class. – juanchopanza Feb 21 '13 at 13:30
  • @juanchopanza thanks for the info btw, I never used std::thread. – UmNyobe Feb 21 '13 at 13:31
  • thread is a concept, `std::thread` is a class. assuming that the generic term can be appropriated by a new facility is just wrong. – Javier Feb 21 '13 at 13:32
  • @Javier whatever. People often say string when they mean `std::string`. They could also mean an abstract concept, or a null-terminated `char` array. One just has to be aware of the different possible meanings (I always use `std::whatever` to disambiguate). – juanchopanza Feb 21 '13 at 13:39
  • I also have updated the case I used with thread but I also got an error? – olidev Feb 21 '13 at 13:41

4 Answers4

4

First of all, what is the difference between thread and pthread.

pthread is a threading library implementation, accessible in C, C++ and other languages.

thread is std::thread, a C++ object (part of C++11) - at least that's what I assume you mean by thread.

What should I use with in C++.

If you have a compiler supporting it, use std::thread. Otherwise, see if you can use boost::thread. If none of these two are good options for whatever reasons (your project is not allowed to use boost, you've got to use an old C++ compiler, etc), then pthread is a good alternative.

How can I solve this [specific compilation] problem?

Your implementation attempts to pass a C++ object member function as a free function pointer (that shouldn't work). You should create a free function instead that calls your object function.

edit [std::thread example]:

class MyPatcher
{
   public:
     void createThread();

   private:
     void Checkingdata();
}

// MyPatcher.cpp
#include "MyPatcher.h"
#include <thread>
#include <functional> // std::bind
using namespace std;

void MyPatcher::createThread()
{
    // std::bind -> return a functor with signature void functor(void);
    std::thread threadDataReading(std::bind(&MyPatcher::Checkingdata, this));

    threadDataReading.join();
}

void MyPatcher::Checkingdata()
{
}
utnapistim
  • 26,809
  • 3
  • 46
  • 82
1

pthread_create() requires a plain C-style function pointer. You're passing it a member function pointer instead, which cannot be called without the (normally implicit) this parameter.

You can make it work by defining a little wrapper that is a plain function:

static void * Checkingdata(void *arg)
{
  static_cast<MyPatcher*>(arg)->CheckingData();
}

Then you pass that function to pthread_create(), along with the address of an instance of your class as the last argument (whereas you had NULL).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • @MichaelWild: you're incorrect. http://stackoverflow.com/questions/310451/should-i-use-static-cast-or-reinterpret-cast-when-casting-a-void-to-whatever – John Zwinck Feb 21 '13 at 13:34
  • You're right, sorry. Mixed it up with function pointers. Sorry for the confusion – Michael Wild Feb 21 '13 at 13:39
1

Your function that you pass to pthread_create must not be a member function. You can solve this by making a static function. Since you are passing NULL as the argument, I'll just reuse the void *arg to pass the object:

 static void * Checkingdata(void *self)
 {
      MyPatcher *me = reinterpret_cast<MyPatcher*>(self);
      me->DoCheckingData();
 }

 void * DoCheckingdata();   // Does the things you want to do. 

The reason for this is that the thread function doesn't have a "this-pointer" to pass as a hidden argument to the memberfunction.

There are other alternatives, such as using std::thread, which takes a std::function object as it's argument - or something that can be converted to a td::function.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

If you are working on a green-fields project, and you are working on a platform with a c++11 compliant compiler available, I strongly suggest considering the use of std::thread. As the namespace suggests, threading support is now built into the standard lib. Google "C++11 threads" for plenty of tutorial information.

learnvst
  • 15,455
  • 16
  • 74
  • 121
  • if I do this: thread pThread(Checkingdata); with void Checkingdata() defined in the header file; I got this: error: no matching function for call to 'std::thread::thread()' – olidev Feb 21 '13 at 13:36