Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about using pthread in cpp

Reply
Thread Tools

about using pthread in cpp

 
 
KDr2
Guest
Posts: n/a
 
      06-24-2007


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?

--
http://kdr2.net

------yours Killy Draw
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      06-24-2007
KDr2 wrote:
> 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.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      06-24-2007
Juha Nieminen wrote:
> KDr2 wrote:
>> 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
>

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.

--
Ian Collins.
 
Reply With Quote
 
KDr2
Guest
Posts: n/a
 
      06-25-2007
Juha Nieminen <> writes:
many thanks!
> KDr2 wrote:
>> 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.


--
http://kdr2.net

------yours Killy Draw
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      06-26-2007
Ian Collins wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Free online test in C, CPP / Placement papers / CPP,C Interview Questions www.hitechskill.com C++ 0 04-09-2006 10:53 AM
when i compile the cpp file(cmdargs.cpp) int main(int argc, wchar_t* argv[]) Vinu C++ 9 05-05-2005 04:11 AM
Method inlined in source1.cpp and called in source2.cpp Alex Vinokur C++ 7 11-15-2004 09:14 PM
using pthread to receive data on a socket and simultaneously play it using /dev/dsp kanchan C Programming 1 11-13-2004 10:33 PM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57