Multithreading design


Posted on 16th Feb 2014 07:03 pm by admin

Hi

I have come up with a Singleton class that manages a pool of database connections. Basically the pool is a queue in which I have stored some connection objects. When ever a client calls any of the database class methods a connection object is popped from the queue and when done the connection is pushed on the queue. If the there are no more connections then that thread has to wait for signal when the pool has a connection then a signal is broadcasted to the wait thread. And now some code

Code: if(dbPool.size() != 0)
{
pthread_mutex_lock(&criticalSection);
DNativeDb activeDbObj = dbPool.pop();
pthread_mutex_lock(&criticalSection);
...
}First Question: Is it ok to put pop() under the a mutex_lock

elsewhere in code

Code: pthread_mutex_lock(&criticalSection);
dbPool.push(activeObj);
if(dbPool.size() == 1)
{
pthread_cond_broadcast(&cond);
}
thread_mutex_lock(&criticalSection);Second: When my pool has 1 connection object left then I need to broadcast incase some thread that might be waiting can then acquire the object. Is this the right way to achieve pool based connecting handling. Please guide and suggest if I am going wrong anywhere.

Thanks

No comments posted yet

Your Answer:

Login to answer
198 Like 15 Dislike
Previous forums Next forums
Other forums

socket communication between c++/java and sending image
hi,

i have a class in c++ called win32_sockserver which creates socket to java. i am trying

preg_match logical error
Code: <?php
$s = file_get_contents("page.html");
preg_match('/<div cla

Install page
How do I make it so when a user submits information on the Install page, it'll right it into the cor

PHP Session Issue
I'm having an issue with one section on my website dealing with sessions. Now I know that myself and

php libs/ browsercap.in
ok i have a host that refuses to stay current. they control my php settings and libs. is there a way

Bit manipulation program with binary operations
My homework is to create a program to scan in and add binary numbers and print them like so

Pro-rata Billing in Periodic Billing for Rental
Hi guys,

We have a situation, where a rental contract starts at the mid of the month say

Php navigation
I have four buttons on a php page.
If i click a button it will redirect to different php pages.

am I using this for loop correctly
Dear buddies!

Right now I am generating a report with some details for all the dealers.

Need help making login verification cleaner...
I use the below to verify user login. I first check the cookie for the id, ip and a unique id. If it

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash