Re: [Libcds-user] Retrieve value with MichaelHashMap
Concurrent Data Structure library
Brought to you by:
khizmax
|
From: Max K. <kh...@gm...> - 2013-01-30 15:59:08
|
Suppose, your container maps int to struct Foo:
struct Foo {
my_rw_lock lock ; // access lock of some r/w type
std::string str ;
};
typedef cds::container::MichaelHashMap< ... > map_type ;
map_type myMap ;
...
How to apply functor:
struct my_func {
std::string strFound ;
void operator()( map_type::value_type& val )
{
val.lock.lock_read() ;
strFound = val.str ;
val.lock.unlock_read() ;
}
};
my_func f ;
if ( myMap.find( 5, cds::ref(f)) ) {
std::cout << "Value for 5 = " << f.strFound ;
}
else
std::cout << "Not found" ;
In this code cds::ref (or boost::ref) is needed for passing functor by
reference
since the functor contains data.
or, if your compiler supports C++11 lambda:
std::string strFound ;
if ( myMap.find( 5, [&strFound]( map_type::value_type& val) {
val.lock.lock_read() ;
strFound = val.str ;
val.lock.unlock_read() ;
} ) )
{
std::cout << "Value for 5 = " << f.strFound ;
}
else
std::cout << "Not found" ;
BR
Max Khiszinsky
On 01/30/2013 05:26 PM, Lucas Lersch wrote:
> I am using MichaelHashMap following the example in the documentation
> (http://libcds.sourceforge.net/doc/cds-api/classcds_1_1container_1_1_michael_hash_map.html#details).
> I see I can pass a functor as argument to the find() method to
> manipulate the <key,value> pair found. My problem is that I am having
> problems to define such functor and to pass it as argument to the
> find() method.
>
> Can someone provide me a simple example on how to retrieve a value
> from the MichaelHashMap from a key, following the existing example in
> the documentation?
>
> Thank you for the attention. :)
>
> --
> Lucas Lersch
>
>
> ------------------------------------------------------------------------------
> Everyone hates slow websites. So do we.
> Make your web apps faster with AppDynamics
> Download AppDynamics Lite for free today:
> http://p.sf.net/sfu/appdyn_d2d_jan
>
>
> _______________________________________________
> Libcds-user mailing list
> Lib...@li...
> https://lists.sourceforge.net/lists/listinfo/libcds-user
|