about using pthread in cpp

Discussion in 'C++' started by KDr2, Jun 24, 2007.

  1. KDr2

    KDr2 Guest

    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
    KDr2, Jun 24, 2007
    #1
    1. Advertising

  2. 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.
    Juha Nieminen, Jun 24, 2007
    #2
    1. Advertising

  3. KDr2

    Ian Collins Guest

    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.
    Ian Collins, Jun 24, 2007
    #3
  4. KDr2

    KDr2 Guest

    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
    KDr2, Jun 25, 2007
    #4
  5. 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.
    Juha Nieminen, Jun 26, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DrUg13
    Replies:
    1
    Views:
    451
    DrUg13
    Feb 10, 2004
  2. Alex Vinokur
    Replies:
    7
    Views:
    393
    Greg Comeau
    Nov 15, 2004
  3. Vinu
    Replies:
    9
    Views:
    580
  4. kanchan
    Replies:
    1
    Views:
    810
    Karthik Kumar
    Nov 13, 2004
  5. www.hitechskill.com
    Replies:
    0
    Views:
    1,316
    www.hitechskill.com
    Apr 9, 2006
Loading...

Share This Page