I have this code facing performance issues due to locks. See the sketch below (can't post actual code which is too big anyway):
XYZ::process()
{
...
lock();
processSharedData(id );
unlock();
...
}
XYZ::processSharedData(obj id)
{
obj a = accessDataAttr1(id);
//do some integrity checks
//evaluate some conditions and then
func1(attr1, attr2, attr3, ...);
obj b = accessDataAttr2(id);
//do some integrity checks
//evaluate some conditions and then
func2(attr1, attr2, attr3, ...);
obj c = accessDataAttr3(id);
//do some integrity checks
//evaluate some conditions and then
func3(attr1, attr2, attr3, ...);
//do some clean up
return;
}
Now I would like to move out the func1, func2 and func3 from the lock/unlock scope. Please suggest efficient way to do so. Is there a way to store function call with it's parameters to invoke it later?
How about storing all the parameters in a member struct and the function pointers in a std::list?
EDIT:
Sorry for not mentioning this before that c++11 is not yet available to me. Any solution without std:function and std:bind would be great for now.
sure you can do this:
typedef std::vector<std::function<void(void)>> f_list;
XYZ::process() {
//our function list
f_list fl;
lock();
fl=processSharedData(id );
unlock();
//invoke functions
for(auto& f : fl) f();
}
f_list XYZ::processSharedData(obj id) {
f_list fl;
obj a = accessDataAttr1(id);
//do some integrity checks
fl.push_back(
std::bind(&func1, attr1, attr2, attr3)
);
obj b = accessDataAttr2(id);
//do some integrity checks
//evaluate some conditions and then
fl.push_back(
std::bind(&func2, attr1, attr2, attr3)
);
//and so on
return fl;
}
http://en.cppreference.com/w/cpp/utility/functional/bind
http://en.cppreference.com/w/cpp/utility/functional/function
http://en.cppreference.com/w/cpp/container/vector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With