for_each and invoking self's member pointer

Discussion in 'C++' started by Kenneth Porter, Oct 13, 2007.

  1. 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));
    }
    Kenneth Porter, Oct 13, 2007
    #1
    1. Advertising

  2. * 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?
    Alf P. Steinbach, Oct 13, 2007
    #2
    1. Advertising

  3. "Alf P. Steinbach" <> 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.
    Kenneth Porter, Oct 13, 2007
    #3
  4. "Kenneth Porter" <> wrote in message
    news:Xns99C7B0FEC2830shivasewingwitchcom@216.196.97.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.programming.threads/browse_frm/thread/e0c011baf08844c4
    Chris Thomasson, Oct 13, 2007
    #4
  5. "Chris Thomasson" <> wrote in
    news::

    > 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.programming.threads/browse_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.
    Kenneth Porter, Oct 13, 2007
    #5
  6. "Kenneth Porter" <> wrote in message
    news:Xns99C7F01222E38shivawellcom@216.196.97.136...
    > "Chris Thomasson" <> wrote in
    > news::
    >
    >> 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.programming.threads/browse_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?
    Chris Thomasson, Oct 13, 2007
    #6
  7. "Chris Thomasson" <> wrote in
    news::

    > 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.
    Kenneth Porter, Oct 13, 2007
    #7
  8. "Kenneth Porter" <> wrote in message
    news:Xns99C85F1735C2Ashivawellcom@216.196.97.136...
    > "Chris Thomasson" <> wrote in
    > news::
    >
    >> 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.
    Chris Thomasson, Oct 14, 2007
    #8
  9. Kenneth Porter

    Kai-Uwe Bux Guest

    Kenneth Porter wrote:

    > "Alf P. Steinbach" <> 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
    Kai-Uwe Bux, Oct 14, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    8
    Views:
    551
    PJP of NYC
    May 24, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    578
    Lonnie Princehouse
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    592
    Bengt Richter
    Jul 11, 2005
  4. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    397
    Dan Sommers
    Jul 12, 2005
  5. falcon
    Replies:
    0
    Views:
    371
    falcon
    Jul 31, 2005
Loading...

Share This Page