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

Did you know?Explore Trending and Topic pages for more stories like this.
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

Code Review - SQL and Insertion Attacks (Warning: Not for Newbs)
Hey guys,

Its been a while, I know. Use to love coming here to answer peoples questions, but

javascript in php code causes page to display white
Hi All,

Odd one this, I've got the following code (below) which is a simple log in form. The

compile php5 with DOM
Hi,

I can't manage to compile php 5.3.0 from source on Windows to include DOM,
in spite of

Aris, Netweaver BPM, Visual composer and X'app
Dear Experts,

Whats the relationship between the following components: Aris, Netweaver BP

Need help urgant
why down my code ony return one item instead of all the items selected.

$arr = array($date,$

Warning: mysql_num_rows() expects parameter 1
When i add quote at the end of this link
example:http://localhost/articlemania/category.php?cid=3

Sending an SMS Message with ASP.NET
Often times it may be a requirement for your web application to send an SMS message. Such examples w

Database 'Validation'
Hi everyone,
I'm trying to validate the password entered by the user with the password in the dat

How to make URL loads only on login
On my page, when a user login he's redirects to user_home.php. I made it also to open the chat scree

PHP and SMS
Hi all forum members. I am new here and am unsure what category shoild I post this in.
Moderator

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