| | |
Connection Pool -- Locking, Concurrent Access
![]() |
•
•
Join Date: Jul 2008
Posts: 15
Reputation:
Solved Threads: 0
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
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
*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.
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.
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.
C++ Syntax (Toggle Plain Text)
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.
Last edited by Ancient Dragon; Aug 26th, 2008 at 3:01 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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):
Another (classic) solution: use P/V operations with the single private (not public) semaphore:
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.
Pseudocode for non-locking solution (when we return no_available_conn value to requestor):
C++ Syntax (Toggle Plain Text)
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) }
C++ Syntax (Toggle Plain Text)
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 }
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: mid square in hashing using c++
- Next Thread: COM port emulator
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






