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::gregoria n::date_period& );
bool checkPeriod(con st boost::gregoria n::date_period& );
bool addReservation( const boost::gregoria n::date_period& );
int id()const {return ID;}
void delOutOfDate();
bool trylock()
{
return try_lock.try_lo ck();
}
void unlock()
{
try_lock.unlock ();
}
private:
int ID;
bool free_;
typedef std::map<boost: :gregorian::dat e,boost::gregor ian::date_perio d>
Reserved;
Reserved reserved;
Reserved::itera tor iter;
Reserved::itera tor range_start;
Reserved::itera tor range_end;
boost::try_mute x mutex;
boost::try_mute x::scoped_try_l ock try_lock;
};
#endif /*RESOURCE_H_*/
#include "Resource.h "
Resource::Resou rce(int id,bool
free):ID(id),fr ee_(free),iter( reserved.begin( )),try_lock(mut ex,false)
{
}
Resource::~Reso urce()
{
try_lock.unlock ();
}
bool Resource::findA vailiable(const boost::gregoria n::date_period&
period)
{
boost::gregoria n::days days_length=per iod.length();
boost::gregoria n::days size(1);
int length=days_len gth.days();
if(length>15)
{
boost::gregoria n::date_duratio n 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::check Period(const boost::gregoria n::date_period& period)
{
range_start=res erved.lower_bou nd(period.begin ());
range_end=reser ved.upper_bound (period.end());
while(range_sta rt!=range_end)
{
if(period.inter sects(range_sta rt->second))retu rn false;
range_start++;
}
return true;
}
bool Resource::addRe servation(const boost::gregoria n::date_period&
period)
{
range_start=res erved.lower_bou nd(period.begin ());
range_end=reser ved.upper_bound (period.end());
while(range_sta rt!=range_end)
{
if(period.inter sects(range_sta rt->second))retu rn false;
range_start++;
}
reserved.insert (std::pair<boos t::gregorian::d ate,boost::greg orian::date_per iod>(period.beg in(),period));
return true;
}
void Resource::delOu tOfDate()
{
boost::gregoria n::date now(boost::greg orian::day_cloc k::local_day()) ;
while(iter != reserved.end())
{
if(iter->second.last()< now)
{
reserved.erase( iter->second.begin() );
}
}
}
the way I access resources
bool ResourceManager ::reserveResour ce(const
boost::gregoria n::date_period& period)
{
Reserved::itera tor iter;
iter=reserved.b egin();
while(iter!=res erved.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<Resou rceManager>::in stance()->reserveResourc e(some_period);
having this design I can( I hope! ) have n parallel accesses(search ing
/ reserve)
where n is the number of resources.
any sugestion in general?
thanks! 3 2960
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::gregoria n::date_period& ); bool checkPeriod(con st boost::gregoria n::date_period& ); bool addReservation( const boost::gregoria n::date_period& ); int id()const {return ID;} void delOutOfDate(); bool trylock() { return try_lock.try_lo ck(); } void unlock() { try_lock.unlock (); } private: int ID; bool free_; typedef std::map<boost: :gregorian::dat e,boost::gregor ian::date_perio d> Reserved; Reserved reserved; Reserved::itera tor iter; Reserved::itera tor range_start; Reserved::itera tor range_end; boost::try_mute x mutex; boost::try_mute x::scoped_try_l ock try_lock;
};
#endif /*RESOURCE_H_*/
#include "Resource.h "
Resource::Resou rce(int id,bool free):ID(id),fr ee_(free),iter( reserved.begin( )),try_lock(mut ex,false) {
}
Resource::~Reso urce() { try_lock.unlock (); }
bool Resource::findA vailiable(const boost::gregoria n::date_period& period) { boost::gregoria n::days days_length=per iod.length(); boost::gregoria n::days size(1); int length=days_len gth.days(); if(length>15) { boost::gregoria n::date_duratio n 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::check Period(const boost::gregoria n::date_period& period) { range_start=res erved.lower_bou nd(period.begin ()); range_end=reser ved.upper_bound (period.end()); while(range_sta rt!=range_end) { if(period.inter sects(range_sta rt->second))retu rn false; range_start++; } return true; }
bool Resource::addRe servation(const boost::gregoria n::date_period& period) { range_start=res erved.lower_bou nd(period.begin ()); range_end=reser ved.upper_bound (period.end()); while(range_sta rt!=range_end) { if(period.inter sects(range_sta rt->second))retu rn false; range_start++; } reserved.insert (std::pair<boos t::gregorian::d ate,boost::greg orian::date_per iod>(period.beg in(),period)); return true; }
void Resource::delOu tOfDate() { boost::gregoria n::date now(boost::greg orian::day_cloc k::local_day()) ; while(iter != reserved.end()) { if(iter->second.last()< now) { reserved.erase( iter->second.begin() ); } } }
the way I access resources
bool ResourceManager ::reserveResour ce(const boost::gregoria n::date_period& period) { Reserved::itera tor iter; iter=reserved.b egin(); while(iter!=res erved.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
if(iter->second->trylock())
this is the check!
trylock() returns true/false.
bool trylock()//should be exception-safe
{
try{
try_lock.try_lo ck();
return true;
catch(boost::lo ck_error& e){}
return false;
}
I am still waiting for your advises :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 members-only list
|
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 public and I'm wanting
to release a Python program I wrote for review (and critique) and
testing on other platforms, but also I would like to explore the
different software licenses that are available (there seems to be
many). Since the specification...
|
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, whether its Java,
XSLT or Linux, they're a great way of pulling together lots of useful
snippets of code and technique in one place. For the beginner they
provide instant advice, usable code and a way into new areas. They're
also a great way to find...
|
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 http://www.codeproject.com/cpp/notepadre.asp
I'm trying to use best practice in my C++ programming and would appreciate
any advice anyone can give. As code is far more than just a one page
sample, just a review of one of the source files (or even just a function or
two) is...
|
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
| |
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 (i.e. the
get_hash function) is borrowed from various snippets I found on the
net. Thee free function could probably need some love. I have been
thinking about having a second linked list of all entries so that the
cost of freeing is in proportion to...
|
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 to patch the
item in question, and subsequently submitted a patch.)
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
|
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 great news with its
associates that Secure Auditor has been branded as a Five Star product
by SC Magazine August 2008 edition. SC Magazine is among the world’s
most prestigious Information Security magazines.
In the comparative review with...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |