I'm attempting to use std::function as a callback handler.
Such like this,
void some_job( int arg1, std::function<void(int)> & callback )
{
callback( process(arg1) );
}
In this example i used pass-by-reference.
But the problem is, I can't guarantee the std::function
object callback
is still exists because the function executed in asynchronized context in my program.
So I have to manage life cycle of the function object, and I thought two way for it.
using pass-by-value(copy)
using
std::shared_ptr
I know the size of std::function
is fixed in 24 byte in my system and std::shared_ptr
is 8.
But copying std::shared_ptr
cause additional overhead managing it's reference counter.
(Furthermore I can't find any smart and neat way to make a shared_ptr object of std::function
.)
Which way would be better in performance?