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 2958
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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: 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 we have to send another system
|
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...
| |