POSIX - pthread class Implimentation

          I have wandered through many sites to implement thread class, which can be used as generic thread class for specific reuse and user specified thread functions. But I failed to find a class which provide the flexibility (there are many 3rd party thread libraries but implementation is wired and little complicated to use ). But QThread in QT4 framework is more easy and reliable to use. So here Am explaining how to Implement a thread class like QThread. I didn't meant QThread was written in the manner described below but this class can be used similar to a QThread class.
You are familiar with pthread_create(), pthread_join(), pthread_exit() etc Check it here . Normally you need to pass the thread Id, thread attributes, function pointer and arguments as void pointer in to  POSIX thread create function . Am Using Basic thread as POSIX thread itself, for create join etc etc.

class mPthread
{
public:
    mPthread(); // Plain constructor but you can provide arguments if neceassary
    pthread_t threadId; //Common Pthread ID for single instance will help you to save ur face.
    pthread_attr_t threadAttr; // if needed
    virtual void run(); // this is a base class means it can be inherited and run() can be redefiined.
    void start(); //key function for the thread call.
private:
    static void *mThreadFunction(void *arg); //this is the function pointer passing in to pthread_create().
};

/*this function can be re-implemented in its child class */

void mPthread::run()
{
    for(int i=0; i<10;i++)
    {
        cout<<": In Base Class :"<<endl;
        sleep(1);
    }
}
/*this function will start the thread from the user call.*/
void mPthread::start()
{
    pthread_attr_t      attr;
    pthread_attr_init(&attr);
    int ret = pthread_create(&threadId,&attr,mThreadFunction,this);
    cout<<"ret thread:"<<ret<<endl;
}
/*this is the pthread function pointer*/
void *mPthread::mThreadFunction(void *arg)
{
    m_this = arg;
    mPthread* thisObj = (mPthread*)arg;
    thisObj->is_running = true;
    thisObj->run();
    thisObj->is_running = false;
    return NULL;
}

The base class having a start function which will help you to start the thread. If you want to use this class you just need to simply inherit it in your own class to handle the thread. sample implementation is given below.

class threads : public mPthread
{
public:
    threads();
    void run();//this is the run() function with the provision to be redefined. :)
};
void threads::run()
{
    for(int i=0; i<10;i++)
    {
        cout<<":User Run:"<<endl;
        sleep(1);
    }
}
int main()
{
 threads *th = new threads();
 th->start(); // this start will help you to start the thread. out put will be your run function.
}
output:
:User Run:
:User Run:
:User Run:
:User Run:
:User Run:
:User Run:
:User Run:
:User Run:
:User Run:
:User Run:

If run fucntion is not implicated in inherited class out out will be .
: In Base Class :
: In Base Class :
: In Base Class :
: In Base Class :
: In Base Class :
: In Base Class :
: In Base Class :
: In Base Class :
: In Base Class :
: In Base Class :



Extra features like Thread detach , exit etc can also implement in the base class, be careful in using the common class object thread_id for all the above purpose.
Feel free to ask here, for any more explanation.




No comments:

Post a Comment