Reputation: 20624
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()'
Upvotes: 2
Views: 2257
Reputation: 27365
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()
{
}
Upvotes: 4
Reputation: 129314
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
.
Upvotes: 1
Reputation: 16195
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.
Upvotes: 0
Reputation: 249123
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).
Upvotes: 1