about using pthread in cpp

K

KDr2

i just want to put *pthred_create* , *pthread_join* into one class,
but i do not know how to, anyone help me?

the code below is mine:
------------------------------
#include <pthread.h>
#include <iostream>

using namespace std;

class CppThread{
public:
CppThread():num(0),slp(3){}
CppThread(int n,int s):num(n),slp(s){}

void create(){
typedef void* (CppThread::*RUN)(void*);
RUN r=&CppThread::run;
pthread_create(&tid,NULL,(mem_fun(run),r),new int(num));
}

void join(){
if(tid){
pthread_join(tid,NULL);
}
}

private:
pthread_t tid;
int num;
int slp;
void* run(void *i){
sleep(slp);
cout<< "Thread num : " << num <<" \ti:"<< *(static_cast<int*>(i)) << endl;
delete static_cast<int*>(i);
}

};


int main(int argc,char*argv[]){

CppThread ct1(1,10);
CppThread ct2(2,5);
ct1.create();
ct2.create();
ct1.join();
ct2.join();
}

-------------------------------

i compile it with g++,and the msg is:

kdr2@kdr2-pc:~/work/by_lang/c_cpp/stdhk$ g++ cppthread.cpp
cppthread.cpp: In member function ‘void CppThread::create()’:
cppthread.cpp:14: 错误: argument of type ‘void* (CppThread::)(void*)’ does not match ‘void* (CppThread::*)(void*)’
kdr2@kdr2-pc:~/work/by_lang/c_cpp/stdhk$

i think the type [void* (CppThread::)(void*)] and [void* (CppThread::*)(void*)] is the same one,aren't them?
 
J

Juha Nieminen

KDr2 said:
class CppThread{ ....
pthread_create(&tid,NULL,(mem_fun(run),r),new int(num)); ....
void* run(void *i){

The thread function called by pthread_create() cannot be a member
function of a class. It has to be a class function (ie. static) or
a regular function.

The "standard" solution to this is to make your run() function
static and give it the CppThread instance as parameter (by giving
'this' to pthread_create() as the data parameter) and then use

CppThread* instance = reintepret_cast<CppThread*>(i);

at the beginning of run(), after which you can access the class
members through the 'instance' pointer.
 
I

Ian Collins

Juha said:
The thread function called by pthread_create() cannot be a member
function of a class. It has to be a class function (ie. static) or
a regular function.

The "standard" solution to this is to make your run() function
static and give it the CppThread instance as parameter (by giving
'this' to pthread_create() as the data parameter) and then use
This isn't correct, the functions should have extern "C" linkage, which
a static member can not have.

Use a friend function if access the the class innards is required.
 
K

KDr2

Juha Nieminen said:
The thread function called by pthread_create() cannot be a member
function of a class. It has to be a class function (ie. static) or
a regular function.

The "standard" solution to this is to make your run() function
static and give it the CppThread instance as parameter (by giving
'this' to pthread_create() as the data parameter) and then use

CppThread* instance = reintepret_cast<CppThread*>(i);

at the beginning of run(), after which you can access the class
members through the 'instance' pointer.
 
J

Juha Nieminen

Ian said:
This isn't correct, the functions should have extern "C" linkage, which
a static member can not have.

I have used posix threads exactly like I described without any
problems.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top