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

problem with library design: support both std::ptr_fun and functor

P: n/a
Hello, I have a situation where I need to design a library for
multi-thread application, each thread does some work in a client
supplied std::ptr_fun(free_function) or a functor.

Now since it's a multi-threaded application, naturally I want the call
back functor to be created on a per thread basis.

Suppose my thread is so defined

template <typename servlet_type>
struct worker_thread{
void operator()(){
serverlet_type()(int x);
}
};

Now I find it a problem to actually pass a free function to the worker
thread.

void serv(int x){
}

typedef
std::pointer_to_unary_function<int, voidserverlet_type;

worker_thread<serverlet_typethr;

Now, thr does not actually know that I want to use serv to perform per
thread work.

I can pass the free function ptr all the way to worker_thread
constructor but this design conflicts with a functor callback. If a
functor object is passed all the way to worker_thread, the library is
not thread safe.

I am using boost::thread to build this library. But that should be
irrelevant. My problem is that when using a container with
free_function/functor, I run into this problem. I checked STL
implementation of various algorithms, they are all stateless free
functions which made it possible to accept both std::ptr_fun or a user
defined functor.

Any suggestion on this issue? Thank you very much.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 17 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Fei Liu wrote:
Hello, I have a situation where I need to design a library for
multi-thread application, each thread does some work in a client
supplied std::ptr_fun(free_function) or a functor.

Now since it's a multi-threaded application, naturally I want the call
back functor to be created on a per thread basis.

Suppose my thread is so defined

template <typename servlet_type>
struct worker_thread{
void operator()(){
serverlet_type()(int x);
}
};

Now I find it a problem to actually pass a free function to the worker
thread.

void serv(int x){
}

typedef
std::pointer_to_unary_function<int, voidserverlet_type;

worker_thread<serverlet_typethr;

Now, thr does not actually know that I want to use serv to perform per
thread work.

I can pass the free function ptr all the way to worker_thread
constructor but this design conflicts with a functor callback. If a
functor object is passed all the way to worker_thread, the library is
not thread safe.

I am using boost::thread to build this library. But that should be
irrelevant. My problem is that when using a container with
free_function/functor, I run into this problem. I checked STL
implementation of various algorithms, they are all stateless free
functions which made it possible to accept both std::ptr_fun or a user
defined functor.

Any suggestion on this issue? Thank you very much.
reference from Boost.Thread and its samples

It always take boost::function0<voidto construct a thread,
then as a client programmer, we can provide stateless or stateful
functor as adapter to the boost::function0<voidtype.
In this way the thread constructor can be simple and bring more
flexibility to the client programmer.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 20 '07 #2

P: n/a
On Aug 17, 10:14 pm, Fei Liu <fei...@aepnetworks.comwrote:
template <typename servlet_type>
struct worker_thread{
void operator()(){
serverlet_type()(int x);
}
That code doesn't compile, and doesn't even make sense to me.

I can pass the free function ptr all the way to worker_thread
constructor but this design conflicts with a functor callback. If a
functor object is passed all the way to worker_thread, the library is
not thread safe.
A function pointer is just a special case of a functor.

template<typename F>
void do_some_work_with_functor_or_function_pointer(cons t F& f) (or
pass by value, for some reason some crazy people seem to like that)
{
// whatever
f();
}

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Aug 20 '07 #3

P: n/a
Barry wrote:
Fei Liu wrote:
>Hello, I have a situation where I need to design a library for
multi-thread application, each thread does some work in a client
supplied std::ptr_fun(free_function) or a functor.

Now since it's a multi-threaded application, naturally I want the call
back functor to be created on a per thread basis.

Suppose my thread is so defined

template <typename servlet_type>
struct worker_thread{
void operator()(){
serverlet_type()(int x);
}
};

Now I find it a problem to actually pass a free function to the worker
thread.

void serv(int x){
}

typedef
std::pointer_to_unary_function<int, voidserverlet_type;

worker_thread<serverlet_typethr;

Now, thr does not actually know that I want to use serv to perform per
thread work.

I can pass the free function ptr all the way to worker_thread
constructor but this design conflicts with a functor callback. If a
functor object is passed all the way to worker_thread, the library is
not thread safe.

I am using boost::thread to build this library. But that should be
irrelevant. My problem is that when using a container with
free_function/functor, I run into this problem. I checked STL
implementation of various algorithms, they are all stateless free
functions which made it possible to accept both std::ptr_fun or a user
defined functor.

Any suggestion on this issue? Thank you very much.
reference from Boost.Thread and its samples

It always take boost::function0<voidto construct a thread,
then as a client programmer, we can provide stateless or stateful
functor as adapter to the boost::function0<voidtype.
In this way the thread constructor can be simple and bring more
flexibility to the client programmer.
Ok, let's take out boost thread from the picture. servetlet is not used
to start a boost thread. I just want to call this function/functor from
the server thread. But when doing so, I found it difficult to design a
single interface to accommodate both a free function or a functor.
Because a free function is a pointer (value) but a functor can be used
as either a type or a value.

In this particular case, I need a type semantic for free function but
still the created type can hold on to the pointer value when it's
instantiated and invoked from a server thread. Clearly I have no trouble
designing the library with value semantic but it's not thread safe in
the case of functor callback.

Fei
Aug 20 '07 #4

P: n/a
Fei Liu wrote:
Barry wrote:
>Fei Liu wrote:
>>Hello, I have a situation where I need to design a library for
multi-thread application, each thread does some work in a client
supplied std::ptr_fun(free_function) or a functor.

Now since it's a multi-threaded application, naturally I want the
call back functor to be created on a per thread basis.

Suppose my thread is so defined

template <typename servlet_type>
struct worker_thread{
void operator()(){
serverlet_type()(int x);
}
};

Now I find it a problem to actually pass a free function to the
worker thread.

void serv(int x){
}

typedef
std::pointer_to_unary_function<int, voidserverlet_type;

worker_thread<serverlet_typethr;

Now, thr does not actually know that I want to use serv to perform
per thread work.

I can pass the free function ptr all the way to worker_thread
constructor but this design conflicts with a functor callback. If a
functor object is passed all the way to worker_thread, the library is
not thread safe.

I am using boost::thread to build this library. But that should be
irrelevant. My problem is that when using a container with
free_function/functor, I run into this problem. I checked STL
implementation of various algorithms, they are all stateless free
functions which made it possible to accept both std::ptr_fun or a
user defined functor.

Any suggestion on this issue? Thank you very much.
reference from Boost.Thread and its samples

It always take boost::function0<voidto construct a thread,
then as a client programmer, we can provide stateless or stateful
functor as adapter to the boost::function0<voidtype.
In this way the thread constructor can be simple and bring more
flexibility to the client programmer.
Ok, let's take out boost thread from the picture. servetlet is not used
to start a boost thread. I just want to call this function/functor from
the server thread. But when doing so, I found it difficult to design a
single interface to accommodate both a free function or a functor.
I think Boost.function meets your need, you can make
boost::function<void(void)(boost.function0<void>) as member,
look at the code snippet below:

#include <iostream>
#include <boost/function.hpp>

void fvoid()
{
std::cout << "fvoid" << std::endl;
}

struct A
{
void operator() () const {
std::cout << "A::operator()()" << std::endl;
}
};

struct B
{
B(int i) : i_(i) {}
void operator() () const {
std::cout << "B::operator()() " << i_ << std::endl;
}
private:
int i_;
};
int
main()
{
boost::function<void(void)f0 = fvoid;
f0();

A a;
f0 = a;
f0();

B b(10);
f0 = b;
f0();

return 0;
}

so I think you can take boost::function<void(void)(function0<void>) as
your member of the class (serverlet?).
Do I still miss your point?
Anyway, if you wanna write *function* yourself, yeah, it's a difficult
job, at least for me. :-)

Because a free function is a pointer (value) but a functor can be used
as either a type or a value.

In this particular case, I need a type semantic for free function but
still the created type can hold on to the pointer value when it's
instantiated and invoked from a server thread. Clearly I have no trouble
designing the library with value semantic but it's not thread safe in
the case of functor callback.
Aug 21 '07 #5

P: n/a
Barry wrote:
Fei Liu wrote:
>Barry wrote:
>>Fei Liu wrote:
Hello, I have a situation where I need to design a library for
multi-thread application, each thread does some work in a client
supplied std::ptr_fun(free_function) or a functor.

Now since it's a multi-threaded application, naturally I want the
call back functor to be created on a per thread basis.

Suppose my thread is so defined

template <typename servlet_type>
struct worker_thread{
void operator()(){
serverlet_type()(int x);
}
};

Now I find it a problem to actually pass a free function to the
worker thread.

void serv(int x){
}

typedef
std::pointer_to_unary_function<int, voidserverlet_type;

worker_thread<serverlet_typethr;

Now, thr does not actually know that I want to use serv to perform
per thread work.

I can pass the free function ptr all the way to worker_thread
constructor but this design conflicts with a functor callback. If a
functor object is passed all the way to worker_thread, the library
is not thread safe.

I am using boost::thread to build this library. But that should be
irrelevant. My problem is that when using a container with
free_function/functor, I run into this problem. I checked STL
implementation of various algorithms, they are all stateless free
functions which made it possible to accept both std::ptr_fun or a
user defined functor.

Any suggestion on this issue? Thank you very much.

reference from Boost.Thread and its samples

It always take boost::function0<voidto construct a thread,
then as a client programmer, we can provide stateless or stateful
functor as adapter to the boost::function0<voidtype.
In this way the thread constructor can be simple and bring more
flexibility to the client programmer.
Ok, let's take out boost thread from the picture. servetlet is not
used to start a boost thread. I just want to call this
function/functor from the server thread. But when doing so, I found it
difficult to design a single interface to accommodate both a free
function or a functor.

I think Boost.function meets your need, you can make
boost::function<void(void)(boost.function0<void>) as member,
look at the code snippet below:

#include <iostream>
#include <boost/function.hpp>

void fvoid()
{
std::cout << "fvoid" << std::endl;
}

struct A
{
void operator() () const {
std::cout << "A::operator()()" << std::endl;
}
};

struct B
{
B(int i) : i_(i) {}
void operator() () const {
std::cout << "B::operator()() " << i_ << std::endl;
}
private:
int i_;
};
int
main()
{
boost::function<void(void)f0 = fvoid;
f0();

A a;
f0 = a;
f0();

B b(10);
f0 = b;
f0();

return 0;
}

so I think you can take boost::function<void(void)(function0<void>) as
your member of the class (serverlet?).
Do I still miss your point?
Anyway, if you wanna write *function* yourself, yeah, it's a difficult
job, at least for me. :-)
What if it's not a simple void (*fp)(void) function type? Does
boost::function support it? Again, if you check my explanation below.
You'd see that It's a problem with type semantic. Even in your current
example, you assign the value of fvoid to f0. How do you propose to
initialize server in my original program?

Thinking of it, I began to see that maybe there is something
fundamentally flawed with my design. I simply cannot use type semantic
with free function...As it's only meaningful to pass the function
address value around to be invoked later.

It's probably sensible to confine callbacks as functors. It's more
flexible and powerful from my experience to be used as callback.

Fei
>
>Because a free function is a pointer (value) but a functor can be used
as either a type or a value.

In this particular case, I need a type semantic for free function but
still the created type can hold on to the pointer value when it's
instantiated and invoked from a server thread. Clearly I have no
trouble designing the library with value semantic but it's not thread
safe in the case of functor callback.
Aug 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.