DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Connection Pool -- Locking, Concurrent Access (http://www.daniweb.com/forums/thread142310.html)

vadan Aug 26th, 2008 1:06 am
Connection Pool -- Locking, Concurrent Access
 
Sorry for all my previous posts

let me explain the exact problem

There is a singleton object in the shared memory. It conatins a pool of connection objects to a server. (more likely an array of connection objects).
I want to go through the array and find which connection object is free to access.

Then that particular object is locked, used for connection and then unlocked.
The following is the singleton object which encapsulates the connection pool array.

class single
{
private:
~single();
single();
connectionObj[5];
public:
connectionObj & GetConnectionObj() ;
void Lock();
void UnLock();
}

There are at least 8 processes that are simultaneously accessing the singleton object.

I want a synchronization between all these process and want to implement GetconnectionObj() function. How to implement it and where the locks must be used.

Should the locks be while accessing singleton object or while acessing the connection object, or both

please explain me with the help of code

Thanks in advance

Regards
VADAN

Ancient Dragon Aug 26th, 2008 2:57 am
Re: Connection Pool -- Locking, Concurrent Access
 
*nix shared memory with semiphores

Despite my previous statement, I think you only need one semiphore to synchronize access to that array of objects. Semiphones for each array element isn't necessary because you are doing your own reference counting for each connection object.

For any given process, this is what I would do to gain control of a connection object. Similar method to free a connection object, except of course you decrement the reference count.
forever loop
    Get semiphone (wait indefinitely until it is available)
    Is a free connection object available
        Yes, increment connection object, release semiphone, and exit loop
        None available, release semiphone, sleep a few clock ticks to allow context switch,
            then return to top of forever loop
end of forever loop

Under MS-Windows you wouldn't need that shared memory array, just create 5 semiphones, then call WaitForMultipleObjects( ... ) to gain access to the first available semiphone. I don't know if anything similar is available on *nix or not.

ArkM Aug 26th, 2008 3:36 am
Re: Connection Pool -- Locking, Concurrent Access
 
You need a single private (not public) lock in your connections manager to implement critical sections in getConnection() and freeConnection() member functions.
Pseudocode for non-locking solution (when we return no_available_conn value to requestor):
Result ConnnectionManager::getConnection()
{
    begin critical section (lock semaphore/mutex)
    result = no_available_conn
    search connections array for free slot
    if free slot found then
        mark it as busy
        result = selected_conn_handler
    end critical section (unlock semaphore/mutex)
    return result
}

void ConnectionManager::freeConnection(conn_handler)
{
    begin critical section (lock semaphore/mutex)
    find busy slot by conn_handler
    if found then
        mark it as free
    end critical section (unlock semaphore/mutex)
}
Another (classic) solution: use P/V operations with the single private (not public) semaphore:
ConnectionManager::ConnectionManager()
{
    Prepare conn[N] array
    Initialize sem(N)
}
Result ConnectionManager::getConnection()
{
    sem.P(); // lock process if no available connections
    find free slot and return its handler
}
void ConnectionManager::freeConnection(conn_handler)
{
    find slot by conn_handler
    mark it as free
    sem.V(); // signal that free conn is available
}
For P/V operations see, for example:
http://en.wikipedia.org/wiki/Semaphore_(programming)

No standard C++ types and functions for semaphores, mutexes and critical sections.
There are some portable libraries implementing this stuff, for example:
http://www.boost.org/

Avoid public locking functions. Incapsulate these delicate operations as deep as possible.


All times are GMT -4. The time now is 12:31 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC