473,499 Members | 1,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

boost::try_mutex mutex--code review/your advise-comments

g
hello!

here is some code:
#ifndef RESOURCE_H_
#define RESOURCE_H_

#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/thread/mutex.hpp>
#include <map>

class Resource
{
public:
Resource(int id,bool free);
virtual ~Resource();
bool findAvailiable(const boost::gregorian::date_period&);
bool checkPeriod(const boost::gregorian::date_period&);
bool addReservation(const boost::gregorian::date_period&);
int id()const {return ID;}
void delOutOfDate();
bool trylock()
{
return try_lock.try_lock();
}
void unlock()
{
try_lock.unlock();
}
private:
int ID;
bool free_;
typedef std::map<boost::gregorian::date,boost::gregorian:: date_period>
Reserved;
Reserved reserved;
Reserved::iterator iter;
Reserved::iterator range_start;
Reserved::iterator range_end;
boost::try_mutex mutex;
boost::try_mutex::scoped_try_lock try_lock;

};

#endif /*RESOURCE_H_*/

#include "Resource.h"

Resource::Resource(int id,bool
free):ID(id),free_(free),iter(reserved.begin()),tr y_lock(mutex,false)
{

}

Resource::~Resource()
{
try_lock.unlock();
}

bool Resource::findAvailiable(const boost::gregorian::date_period&
period)
{
boost::gregorian::days days_length=period.length();
boost::gregorian::days size(1);
int length=days_length.days();
if(length>15)
{
boost::gregorian::date_duration d(2);
size=d;
}
short a=0;
while(a<3)
{
period.begin()+size;
period.end()-size;
if(checkPeriod(period))return true;
a++;
}
return false;
}

bool Resource::checkPeriod(const boost::gregorian::date_period& period)
{
range_start=reserved.lower_bound(period.begin());
range_end=reserved.upper_bound(period.end());
while(range_start!=range_end)
{
if(period.intersects(range_start->second))return false;
range_start++;
}
return true;
}

bool Resource::addReservation(const boost::gregorian::date_period&
period)
{
range_start=reserved.lower_bound(period.begin());
range_end=reserved.upper_bound(period.end());
while(range_start!=range_end)
{
if(period.intersects(range_start->second))return false;
range_start++;
}
reserved.insert(std::pair<boost::gregorian::date,b oost::gregorian::date_period>(period.begin(),perio d));
return true;
}

void Resource::delOutOfDate()
{
boost::gregorian::date now(boost::gregorian::day_clock::local_day());
while(iter != reserved.end())
{
if(iter->second.last()<now)
{
reserved.erase(iter->second.begin());
}
}
}
the way I access resources
bool ResourceManager::reserveResource(const
boost::gregorian::date_period& period)
{
Reserved::iterator iter;
iter=reserved.begin();
while(iter!=reserved.end())
{
if(iter->second->trylock())
{
if(iter->second->addReservation(period))
{
iter->second->unlock();
return true;
}
iter->second->unlock();
}
iter++;
}
return false;
}
is this thread-safe enough??

I will use a singleton for the ResourceManager
Singleton<ResourceManager>::instance()->reserveResource(some_period);
having this design I can( I hope! ) have n parallel accesses(searching
/ reserve)
where n is the number of resources.
any sugestion in general?

thanks!

Mar 20 '06 #1
3 2922
g wrote:
hello!

here is some code:
#ifndef RESOURCE_H_
#define RESOURCE_H_

#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/thread/mutex.hpp>
#include <map>

class Resource
{
public:
Resource(int id,bool free);
virtual ~Resource();
bool findAvailiable(const boost::gregorian::date_period&);
bool checkPeriod(const boost::gregorian::date_period&);
bool addReservation(const boost::gregorian::date_period&);
int id()const {return ID;}
void delOutOfDate();
bool trylock()
{
return try_lock.try_lock();
}
void unlock()
{
try_lock.unlock();
}
private:
int ID;
bool free_;
typedef std::map<boost::gregorian::date,boost::gregorian:: date_period>
Reserved;
Reserved reserved;
Reserved::iterator iter;
Reserved::iterator range_start;
Reserved::iterator range_end;
boost::try_mutex mutex;
boost::try_mutex::scoped_try_lock try_lock;

};

#endif /*RESOURCE_H_*/

#include "Resource.h"

Resource::Resource(int id,bool
free):ID(id),free_(free),iter(reserved.begin()),tr y_lock(mutex,false)
{

}

Resource::~Resource()
{
try_lock.unlock();
}

bool Resource::findAvailiable(const boost::gregorian::date_period&
period)
{
boost::gregorian::days days_length=period.length();
boost::gregorian::days size(1);
int length=days_length.days();
if(length>15)
{
boost::gregorian::date_duration d(2);
size=d;
}
short a=0;
while(a<3)
{
period.begin()+size;
period.end()-size;
if(checkPeriod(period))return true;
a++;
}
return false;
}

bool Resource::checkPeriod(const boost::gregorian::date_period& period)
{
range_start=reserved.lower_bound(period.begin());
range_end=reserved.upper_bound(period.end());
while(range_start!=range_end)
{
if(period.intersects(range_start->second))return false;
range_start++;
}
return true;
}

bool Resource::addReservation(const boost::gregorian::date_period&
period)
{
range_start=reserved.lower_bound(period.begin());
range_end=reserved.upper_bound(period.end());
while(range_start!=range_end)
{
if(period.intersects(range_start->second))return false;
range_start++;
}
reserved.insert(std::pair<boost::gregorian::date,b oost::gregorian::date_period>(period.begin(),perio d));
return true;
}

void Resource::delOutOfDate()
{
boost::gregorian::date now(boost::gregorian::day_clock::local_day());
while(iter != reserved.end())
{
if(iter->second.last()<now)
{
reserved.erase(iter->second.begin());
}
}
}
the way I access resources
bool ResourceManager::reserveResource(const
boost::gregorian::date_period& period)
{
Reserved::iterator iter;
iter=reserved.begin();
while(iter!=reserved.end())
{
if(iter->second->trylock())
{
if(iter->second->addReservation(period))
{
iter->second->unlock();
return true;
}
iter->second->unlock();
}
iter++;
}
return false;
}
is this thread-safe enough??


I don't see any thing checking for a successful lock.
try_lock logic should be used with is_lock logic to verify the lock is
successful.
If it's not going to check, then you should use lock instead of try_lock

Mar 20 '06 #2
g
if(iter->second->trylock())

this is the check!
trylock() returns true/false.

Mar 20 '06 #3
g
bool trylock()//should be exception-safe
{
try{
try_lock.try_lock();
return true;
catch(boost::lock_error& e){}
return false;
}

I am still waiting for your advises :-)

Mar 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1424
by: gs-code-review-bounces | last post by:
Your mail to 'gs-code-review' with the subject Re: Application Is being held until the list moderator can review it for approval. The reason it is being held: Post by non-member to a...
31
2667
by: poisondart | last post by:
Hi, I'm not sure if this is the right group to post this. If not, then I would appreciate if somebody could point me to the correct group. This is my first time releasing software to the...
0
1202
by: TechBookReport | last post by:
TechBookReport (http://www.techbookreport.com) has just published a review of the Python Cookbook. This is an extract from the full review: We're big fans of cookbooks here at TechBookReport,...
18
2120
by: Ben Hanson | last post by:
I have created an open source Notepad program for Windows in C++ that allows search and replace using regular expressions (and a few other extras). It is located at...
192
9298
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
21
3167
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code...
4
1755
by: Kevin Walzer | last post by:
How long does it take for a patch at the Python SF tracker to be reviewed and/or committed? I am unfamiliar with how the process works. (I originally submitted a bug report, then figured out how...
0
1622
by: corey | last post by:
Secure Bytes audit and vulnerability assessment software Secure Auditor named “Versatile tool” and earn “Five Star Ratings” in SC Magazine Group Test Secure Bytes is really pleased to share this...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4920
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.