By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,702 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

binders to members of members

P: n/a
consider the following piece of code:

void MyClass::VideoThreadLocker::operator()(Threadable * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.lock();
};

void MyClass::VideoThreadUnlocker::operator()(Threadabl e * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.unlock();
};

void MyClass::processData(PipeData & data)
{
std::for_each(threads.begin(),threads.end(),VideoT hreadLocker());
cond.signalAll();
...
std::for_each(threads.begin(),threads.end(),VideoT hreadUnlocker());
}

is there a way to avoid the creation of classes VideoThreadLocker and
VideoThreadUnlocker?

I can't figure out how to use mem_fun and binders in order to achieve
the static_cast and reach mutex.lock() or mutex.unlock()

is pointers to member-functions a good alternative? Using this, I will
create one additional class (e.g.: MutexThreadOper), intead of the two
current classes

cheers
Diego Martins

Nov 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 22 Nov 2005 09:05:10 -0800, "Diego Martins" <jo********@gmail.com>
wrote:
consider the following piece of code:

void MyClass::VideoThreadLocker::operator()(Threadable * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.lock();
};

void MyClass::VideoThreadUnlocker::operator()(Threadabl e * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.unlock();
};

void MyClass::processData(PipeData & data)
{
std::for_each(threads.begin(),threads.end(),VideoT hreadLocker());
cond.signalAll();
...
std::for_each(threads.begin(),threads.end(),VideoT hreadUnlocker());
}

is there a way to avoid the creation of classes VideoThreadLocker and
VideoThreadUnlocker?

I can't figure out how to use mem_fun and binders in order to achieve
the static_cast and reach mutex.lock() or mutex.unlock()

is pointers to member-functions a good alternative? Using this, I will
create one additional class (e.g.: MutexThreadOper), intead of the two
current classes

cheers
Diego Martins


It's very hard to say anything without seeing the declarations of
these classes. Can you post the relevant parts of the headers? Also,
how are you creating your threads? Are any of these objects in shared
libraries? Lots of variables here.

--
Bob Hairgrove
No**********@Home.com
Nov 23 '05 #2

P: n/a
Diego Martins wrote:
consider the following piece of code:

void MyClass::VideoThreadLocker::operator()(Threadable * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.lock();
};

void MyClass::VideoThreadUnlocker::operator()(Threadabl e * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.unlock();
};

void MyClass::processData(PipeData & data)
{
std::for_each(threads.begin(),threads.end(),VideoT hreadLocker());
cond.signalAll();
...
std::for_each(threads.begin(),threads.end(),VideoT hreadUnlocker());
}

is there a way to avoid the creation of classes VideoThreadLocker and
VideoThreadUnlocker?

I can't figure out how to use mem_fun and binders in order to achieve
the static_cast and reach mutex.lock() or mutex.unlock()

is pointers to member-functions a good alternative? Using this, I will
create one additional class (e.g.: MutexThreadOper), intead of the two
current classes


The std functions do not bind to data members. Boost's bind() (now part
of std::tr1) can bind to data members, but then there is still the
locking methods to call.

A simpler approach would be to add lock() and unlock() methods directly
to Threadable's interface and have them lock and unlock the data member
mutex themselves. In that way, std::mem_fun(&Threadable::lock) and
unlock could be used in these loops instead of the custom functor
currently in place.

Greg

Nov 23 '05 #3

P: n/a
hmm can this Boost binder do something like this?

void MyClass::processData(PipeData & data)
{

std::for_each(threads.begin(),threads.end(),mem_da ta(&Threadable::mutex,&Mutex::lock));
cond.signalAll();
...

std::for_each(threads.begin(),threads.end(),mem_da ta(&Threadable::mutex,&Mutex::unlock));

}

&Threadable::mutex is a pointer to member-data (which is from Mutex
class)
&Mutex::lock and &Mutex::unlock are pointers to member functions of
Mutex class

Cheers
Diego Martins

Nov 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.