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(fr ee_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_t ype;
worker_thread<s erverlet_typeth r;
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++.m oderated. First time posters: Do this! ] 5 2532
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(fr ee_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_t ype;
worker_thread<s erverlet_typeth r;
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::function 0<voidto construct a thread,
then as a client programmer, we can provide stateless or stateful
functor as adapter to the boost::function 0<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++.m oderated. First time posters: Do this! ]
On Aug 17, 10:14 pm, Fei Liu <fei...@aepnetw orks.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<typena me F>
void do_some_work_wi th_functor_or_f unction_pointer (const 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++.m oderated. First time posters: Do this! ]
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(fr ee_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_t o_unary_functio n<int, voidserverlet_t ype;
worker_thread< serverlet_typet hr;
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_functio n/functor, I run into this problem. I checked STL implementati on 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::function 0<voidto construct a thread,
then as a client programmer, we can provide stateless or stateful
functor as adapter to the boost::function 0<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
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(fr ee_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_functi on<int, voidserverlet_t ype;
worker_thread <serverlet_type thr;
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_functi on/functor, I run into this problem. I checked STL implementatio n 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::function 0<voidto construct a thread, then as a client programmer, we can provide stateless or stateful functor as adapter to the boost::function 0<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)(boo st.function0<vo id>) 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)(fun ction0<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.
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(fr ee_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_funct ion<int, voidserverlet_t ype;
worker_threa d<serverlet_typ ethr;
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 constructo r 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_functio n/functor, I run into this problem. I checked STL implementati on 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::function 0<voidto construct a thread, then as a client programmer, we can provide stateless or stateful functor as adapter to the boost::function 0<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)(boo st.function0<vo id>) 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)(fun ction0<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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Stippler |
last post by:
Hi,
simple question. Why does this not work:
#include <algorithm>
#include <cmath>
#include <iostream>
int
main()
|
by: daniel.w.gelder |
last post by:
I wrote a template class that takes a function prototype and lets you
store and call a C-level function, like this:
inline string SampleFunction(int, bool) {..}
functor<string (int, bool)> myFunctor = SampleFunction;
string result = myFunctor(7, true);
Works great thanks to the help from this group. Here's the guts so far
for two-arity:
|
by: nan.li.g |
last post by:
Hello, all,
This is probably not so much related to C++ (Sorry in advance). But
I did get this question during a C++ job interview.
You are asked to design some data structure and algorith for the
following function:
List getNames ( List nameList, String prefix );
What it does is: given a name list and a prefix, return all the
names in the name list that start with prefix.
|
by: Lionel B |
last post by:
I have a function which takes a functor argument. I which to call it for a
functor which is actually a class member; this works fine, using
the mem_fun_ref and bind1st functions (see listing 1 below). Or, rather,
it works fine as long as my member functor is const. The problem comes
when I wish to use it for a *non*-const functor (see listing 2 below):
*** Start listing 1 ***************************************************
// test1.cpp
|
by: DevNull |
last post by:
Hi there everyone,
I'm creating a very simple immediate mode command interpreter.
The final purpose is to provide a pluggable control and command
console for a MUD server I have written.
The basic theory is we wrap the functions we want exposed to the
console in a function with a prototype of
int func(State*)
| |
by: Daniel Pitts |
last post by:
I have std::vector<Base *bases;
I'd like to do something like:
std::for_each(bases.begin(), bases.end(), operator delete);
Is it possible without writing an adapter? Is there a better way? Is
there an existing adapter?
Thanks,
Daniel.
|
by: Soumen |
last post by:
I wanted convert a mixed case string to a lower case one. And I tried
following code:
std::transform(mixedCaseString.begin(), mixedCaseString::end(),
mixedCaseString.begin(), std::ptr_fun(tolower));
Even though I's including cctype and algorithm, I's getting compiler (g
++ 3.3.6) error:
no matching function for call to `ptr_fun(<unknown type>)'
|
by: ma740988 |
last post by:
Consider
# include <iostream>
# include <vector>
# include <typeinfo>
# include <string>
class BaseMsg {
friend std::ostream& operator<<( std::ostream&, const BaseMsg& );
std::string name ;
|
by: eiji.anonremail |
last post by:
I would like to search in a vector depending on "a" or "a and b"
without the overhead of creating a Foo object or another predicate
class.
Is that possible? Using a member function pointer or a global static
function?
Right now I have this, but I don't like it.
------------------------------Example
code----------------------------------------------
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |