Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C

Mapping POSIX threads to C++ code

2.85/5 (5 votes)
8 Jun 2008CPOL1 min read 2   208  
Mapping POSIX threads to object oriented C++ code.

Introduction

This article describes how to map the native C POSIX threading model into an object orientated model for C++.

POSIX Threads

Historically, there was no common thread interface for different Operating Systems. Vendors implemented their own versions of thread programming interfaces. To make use of all the capabilities of threads on different UNIX versions, and later on, on all different types of Operating Systems, the API had to get implemented with a standard thread interface.

In 1995, the first standard on POSIX threads was implemented. Today, most vendors of hardware and/or Operating Systems make additional implementations compatible to the POSIX thread API.

Using the Code

The code wraps around the standard C interface of the POSIX thread start function and creates a C++ class encapsulating the native C code and defining non-static members as the client interface. So, the code gets fully object oriented instead of using static functions of the C programming interface.

The C++ class MyThread is abstract, so it has to to derived and the OnStart method has to be implemented. The method is called when the thread starts processing, so it is much like the main thread function. If this function returns, the thread exits as well.

The following code shows the header file of the MyThread class:

C++
#define NWTHREAD_EXIT_STATE_OK        0
#define NWTHREAD_EXIT_STATE_ERR       1

#define T_SUCCESS                    0
#define T_OUT_OF_MEMORY            10
#define T_ERR_CREATE_THREAD        11

extern "C"
{
#include <pthread.h>
}

class ThreadArgs
{
    friend class MyThread;

    void *       pArgs;
    MyThread * pThis;
};

class MyThread
{

public:

    enum ThreadState
    {
        Stopped = 0,
        Running = 1
    };

private:

    ThreadState    m_ThreadState;
    pthread_t    m_ThreadHandle;
    long long    m_lThreadID;
    ThreadArgs *  m_pArgs;

public:

    MyThread();
    virtual ~MyThread();

    long long GetThreadID() { return m_lThreadID; }
    void SetThreadID(long long iId) { m_lThreadID = iId; }

    virtual int Start(void * args);
    ThreadState GetThreadState() { return m_ThreadState; }

    int GetExitState() { return m_ThreadExitState; }

    void ExitThread();


protected:

    int            m_ThreadExitState;

    virtual void OnStart(void * arg) = 0;
    virtual void OnExit() { m_ThreadState = Stopped; }

private:

    void * pArgs;
    static void * ThreadStartWrapper(void * pThis);

};

As you can see in this header file, the class wraps around the C-code from the POSIX API using the ThreadStartWrapper method.

Ideas

One thing to think of is to generate a template class out of MyThread with the m_lThreadID as a template argument.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)