Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > for_each and invoking self's member pointer

Reply
Thread Tools

for_each and invoking self's member pointer

 
 
Kenneth Porter
Guest
Posts: n/a
 
      10-13-2007
I'm trying to create a class that represents a bucket of locks, and I
want to lock a set of objects specified by a container of pointers. I
can't seem to find the right combination of function adapters to invoke
the bucket's member function on each pointer in the argument container.
What's the right thing to use here, at the "???"?

#include <vector>
#include <functional>
#include <algorithm>

using namespace std; // keeps example simple

class BucketOfLocks;

class Lockable
{
BucketOfLocks* owner;
public:
void acquire(BucketOfLocks& newOwner) { owner = &newOwner; }
};

class BucketOfLocks
{
public:
BucketOfLocks(const vector<Lockable*>& lockables);
private:
// this is what I want to invoke in the for_each
void acquire(Lockable& lockable);
};

BucketOfLocks::BucketOfLocks(const vector<Lockable*>& lockables)
{
for_each(lockables.begin(), lockables.end(),
???(&BucketOfLocks::acquire,*this));
}
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      10-13-2007
* Kenneth Porter:
> I'm trying to create a class that represents a bucket of locks, and I
> want to lock a set of objects specified by a container of pointers. I
> can't seem to find the right combination of function adapters to invoke
> the bucket's member function on each pointer in the argument container.
> What's the right thing to use here, at the "???"?
>
> #include <vector>
> #include <functional>
> #include <algorithm>
>
> using namespace std; // keeps example simple
>
> class BucketOfLocks;
>
> class Lockable
> {
> BucketOfLocks* owner;
> public:
> void acquire(BucketOfLocks& newOwner) { owner = &newOwner; }
> };
>
> class BucketOfLocks
> {
> public:
> BucketOfLocks(const vector<Lockable*>& lockables);
> private:
> // this is what I want to invoke in the for_each
> void acquire(Lockable& lockable);
> };
>
> BucketOfLocks::BucketOfLocks(const vector<Lockable*>& lockables)
> {
> for_each(lockables.begin(), lockables.end(),
> ???(&BucketOfLocks::acquire,*this));
> }


class BucketOfLocks
{
public:
BucketOfLocks(const vector<Lockable*>& lockables);
private:
// this is what I want to invoke in the for_each
void acquire(Lockable* lockable) {}
};

BucketOfLocks::BucketOfLocks(const vector<Lockable*>& lockables)
{
for_each(lockables.begin(), lockables.end(),
bind1st( mem_fun<void>( &BucketOfLocks::acquire ), this )
);
}

At least it compiles; I haven't tested it.

By the way, why are you using for_each, why not simply use a for loop?


Cheers, & hth.,

- Alf



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Kenneth Porter
Guest
Posts: n/a
 
      10-13-2007
"Alf P. Steinbach" <(E-Mail Removed)> wrote in news:13h04ulpeupb8f2
@corp.supernews.com:

> By the way, why are you using for_each, why not simply use a for loop?


Flexibility. I can change the container type without changing the code that
iterates over it.

Alas, I need to continue to use VC6 to support some customers and will need
to cripple my STL template usage a bit for awhile.
 
Reply With Quote
 
Chris Thomasson
Guest
Posts: n/a
 
      10-13-2007

"Kenneth Porter" <(E-Mail Removed)> wrote in message
news:Xns99C7B0FEC2830shivasewingwitchcom@216.196.9 7.142...
> I'm trying to create a class that represents a bucket of locks, and I
> want to lock a set of objects specified by a container of pointers.


[...]

You are defining a locking order right? Or, are you using unordered
two-phased locking? The following might be of interest to you:

http://groups.google.com/group/comp....c011baf08844c4


 
Reply With Quote
 
Kenneth Porter
Guest
Posts: n/a
 
      10-13-2007
"Chris Thomasson" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> You are defining a locking order right? Or, are you using unordered
> two-phased locking? The following might be of interest to you:
>
> http://groups.google.com/group/comp....rowse_frm/thre
> ad/e0c011baf08844c4


A "lock" in the conventional mutex sense may be a misnomer here. Attempting
to acquire a held lock in my system is considered an error, not something
to be waited for.
 
Reply With Quote
 
Chris Thomasson
Guest
Posts: n/a
 
      10-13-2007
"Kenneth Porter" <(E-Mail Removed)> wrote in message
news:Xns99C7F01222E38shivawellcom@216.196.97.136.. .
> "Chris Thomasson" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> You are defining a locking order right? Or, are you using unordered
>> two-phased locking? The following might be of interest to you:
>>
>> http://groups.google.com/group/comp....rowse_frm/thre
>> ad/e0c011baf08844c4

>
> A "lock" in the conventional mutex sense may be a misnomer here.
> Attempting
> to acquire a held lock in my system is considered an error, not something
> to be waited for.


Okay. Sorry for all the questions, but, are your using traditional
two-phased locking based on try_lock/back-off logic?

 
Reply With Quote
 
Kenneth Porter
Guest
Posts: n/a
 
      10-13-2007
"Chris Thomasson" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Okay. Sorry for all the questions, but, are your using traditional
> two-phased locking based on try_lock/back-off logic?


No, I just throw a busy exception if the lock is already held, and the
application pops up an error. Scheduling of resources is supposed to be
handled elsewhere, and a lock failure at this level indicates a programming
error.
 
Reply With Quote
 
Chris Thomasson
Guest
Posts: n/a
 
      10-13-2007
"Kenneth Porter" <(E-Mail Removed)> wrote in message
news:Xns99C85F1735C2Ashivawellcom@216.196.97.136.. .
> "Chris Thomasson" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>> Okay. Sorry for all the questions, but, are your using traditional
>> two-phased locking based on try_lock/back-off logic?

>
> No, I just throw a busy exception if the lock is already held, and the
> application pops up an error. Scheduling of resources is supposed to be
> handled elsewhere, and a lock failure at this level indicates a
> programming
> error.


Okay. I see.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-14-2007
Kenneth Porter wrote:

> "Alf P. Steinbach" <(E-Mail Removed)> wrote in news:13h04ulpeupb8f2
> @corp.supernews.com:
>
>> By the way, why are you using for_each, why not simply use a for loop?

>
> Flexibility. I can change the container type without changing the code
> that iterates over it.

[snip]

That can be achieved using just a typedef:

typedef std::vector< ticket > ticket_sequence;
...
for ( ticket_sequence::iterator iter = ... ) {
...
}

Now you can change from vector to deque by modifying a single line.


In fact, one can consider using std::vector< whatever > directly instead of
using a typedef to be the typesystem analogue of using a magic number like
12341 instead of a named constant

int const number_of_runs = 12341;


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
for_each and member function Przemyslaw Koprowski C++ 4 02-01-2008 05:16 PM
invoking member functions without creating an object or pointer of the class? ypjofficial@indiatimes.com C++ 22 03-23-2006 11:04 AM
Passing pointer to member function to different member function that then calls for_each pookiebearbottom@yahoo.com C++ 8 05-24-2005 01:50 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM



Advertisments