Click here to Skip to main content
13,202,706 members (56,080 online)
Click here to Skip to main content
Add your own
alternative version

Stats

19.4K views
178 downloads
6 bookmarked
Posted 8 Jun 2008

Mapping POSIX threads to C++ code

, 8 Jun 2008
Rate this:
Please Sign up or sign in to vote.
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:

#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 <span class="code-keyword"><pthread.h></span>
}

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)

Share

About the Author

FlorianPraxmair
Software Developer (Senior)
Austria Austria
Im a graduate student of the Technical University of Graz (Austria). I am also working for a company in Austria where I am developing biometric solutions and algorithms mostly for fingerprint recognition.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralPthread Pin
alaa.ali198127-Sep-10 7:22
memberalaa.ali198127-Sep-10 7:22 
Generalit needs a little more beef to it ... Pin
Maximilien8-Jun-08 6:26
memberMaximilien8-Jun-08 6:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171020.1 | Last Updated 8 Jun 2008
Article Copyright 2008 by FlorianPraxmair
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid