binders to members of members

D

Diego Martins

consider the following piece of code:

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

void MyClass::VideoThreadUnlocker::eek:perator()(Threadable * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.unlock();
};

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

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
 
B

Bob Hairgrove

consider the following piece of code:

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

void MyClass::VideoThreadUnlocker::eek:perator()(Threadable * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.unlock();
};

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

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.
 
G

Greg

Diego said:
consider the following piece of code:

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

void MyClass::VideoThreadUnlocker::eek:perator()(Threadable * thread) {
static_cast<BasicVideoThread *>(thread)->mutex.unlock();
};

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

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
 
D

Diego Martins

hmm can this Boost binder do something like this?

void MyClass::processData(PipeData & data)
{

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

std::for_each(threads.begin(),threads.end(),mem_data(&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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top