_Concurrent Database Commands and C++_
by Harold R. Kasperink and John C. Dekker


Listing One
class CThread;
void ThreadFunc(CThread *);

class CThread 
{
public:
        CThread();
        virtual ~CThread();

        void    Create();
        void    Cancel();
        void    Detach();
        void    Join();

        friend  void    ThreadFunc(CThread *);
        virtual void    Process(void) = 0;
private:
        pthread_t       m_Thread;
};


Listing Two
class CMutex 
{
public:
        CMutex();
        ~CMutex();
        void    Lock();
        void    Unlock();
        int     TryLock();
private:
        pthread_mutex_t         m_Mutex;
};


Listing Three
#define MAX_NR_DBASES   15

class CArrayDbase 
{
private:
    // Pointer to global database connection
    static CArrayDbase      *g_pDbArray;                    
    // Mutex to make sure only one reserve/release at a time
    static CMutex           g_mtxArray;                 
    // Array of database connections
    CDbase*             m_pDbases[MAX_NR_DBASES];   
    // Number of database connections
    int             m_nNrDbases;
public:
    CArrayDbase(int nNrConnections, const char *szUsr, 
                                   const char *szPasswd, const char *szDB);
    virtual ~CArrayDbase();
    // Get pointer to free database connection and reserve database connnection
    static  CDbase*     ReserveDbase();
    // Release database connection
    static  void        ReleaseDbase(CDbase &dbase);
private:
    static void         Lock();
    static void         Unlock();
};


Listing Four
{
private:
    friend class    CArrayDbase;

    // Locks usage of object
    CMutex      m_mtxInuse; 
    // Instruct dbase-thread to do command
    CMutex      m_mtxStartSql;  
    // Informs instruct-thread command is done
    CMutex      m_mtxEndSql;    
    // Make sure no parallel usage of do 
    CMutex      m_mtxDoCmd;     
    // boolean flag indicates if connected
    boolean         m_bConnected;   
    // Continue flag
    boolean         m_bContinue;    
    // Current command
    CDbCommand*     m_pCommand;     
    // Return code of command execution
    long            m_lSql;         
public:
    CDbase();
    virtual ~CDbase();
    // Command functions
    long            Do(CDbCommand &command);
private:
    // Thread loop overwrite
    virtual void    Process();
    // Inuse Funtions
    boolean         TryLock();
    void            Lock();
    void            Unlock();
};

Listing Five
class CDbCommand
{
private:
    friend class CDbase;
    CDbase *    m_pDbase;
protected:
    long        m_lSql;
public:
    CDbCommand();
    CDbCommand(CDbase &dbase);
    virtual ~CDbCommand();

    virtual void    Do() = 0;

    // Get and set functions
   CDbase *        Dbase();
    void            Dbase(CDbase &dbase);
private:
    // Is called by database object to instruct command to go
    virtual long    Execute() = 0;
};

