![]() |
| ||
| 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 |
| ||
| 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 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. |
| ||
| 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()Another (classic) solution: use P/V operations with the single private (not public) semaphore: ConnectionManager::ConnectionManager()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