I have a lot of code like this:
VOID CALLBACK TimerRoutine(PVOID lpParam)
{
long nTaskid = (long)lpParam;
GObj *obj;
if( mapThreadSafe.find( nTaskid, obj )) // mapThreadSafe is a
hash_map, thread safe
{
obj->invoke();
}
}
someone think it's inefficient and code like this:
VOID CALLBACK TimerRoutine(PVOID lpParam)
{
GObj *obj = (GObj *)lpParam;
obj->invoke();
obj->Release();
}
I was confused.I think a hash_map is good enough and payable.
it can avoid a lot of problem in a complex environment.
what is your position? 12 3642
* Darwin Lalo: I have a lot of code like this:
VOID CALLBACK TimerRoutine(PVOID lpParam) { long nTaskid = (long)lpParam;
GObj *obj; if( mapThreadSafe.find( nTaskid, obj )) // mapThreadSafe is a hash_map, thread safe { obj->invoke(); } }
someone think it's inefficient and code like this:
VOID CALLBACK TimerRoutine(PVOID lpParam) { GObj *obj = (GObj *)lpParam; obj->invoke(); obj->Release(); }
I was confused.I think a hash_map is good enough and payable. it can avoid a lot of problem in a complex environment. what is your position?
I think it's a good idea to get rid of the void pointers (isolate any
essential usage in some common small thing down in the depths), the C
style casts (unsafe for maintenance), the misleading and generally
unreadable Hungarian prefix notation (which has no advantage today), the
unnecessary macros like VOID for void (and macros in general), and the
manual redundant resource management like obj->Release (replace with
smart pointers, above you have both a deallocation responsibility
problem and an exception unsafety problem, both solved by better way).
That will improve the code a lot, I think.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
obj->Release is bad. ref_count_ptr is good. I agree.
but sometime System API need a PVOID parameter like
BOOL CreateTimer( TIMERCALLBACK Callback, PVOID Parameter, DWORD
DueTime)
The Parameter above can't be ref_count_ptr. how do I?
like this?
? struct TimerParameter{
ref_count_ptr<GObj> obj;
};
CALL {
TimerParameter *p = new TimerParameter;
CreateTimer(TimerRoutine, p, 10);
}
VOID CALLBACK TimerRoutine(PVOID lpParam)
{
TimerParameter *p = (TimerParameter *)lpParam;
ref_count_ptr<GObj> obj = p->obj;
delete p;
obj->invoke();
}
thank you,maybe I am newbie. :)
Darwin Lalo wrote: obj->Release is bad. ref_count_ptr is good. I agree. but sometime System API need a PVOID parameter like
You should quote context in your reply, not everyone uses google!
BOOL CreateTimer( TIMERCALLBACK Callback, PVOID Parameter, DWORD DueTime)
The Parameter above can't be ref_count_ptr. how do I?
You can't in this case, you're stuck with what the API requires. Even
though the API uses ugly macros for parameter types, you code will
probably be easier to read if you don't.
Back to your original question, follow Alf's advice on variable names.
Considering the above CreateTimer binds an object (Parameter), to a
function (Callback), why bother with the map?
VOID CALLBACK TimerRoutine(PVOID lpParam) { TimerParameter *p = (TimerParameter *)lpParam;
Don't use C casts, in this case, use static_cast.
--
Ian Collins.
Ian Collins wrote: Darwin Lalo wrote: obj->Release is bad. ref_count_ptr is good. I agree. but sometime System API need a PVOID parameter like You should quote context in your reply, not everyone uses google!
BOOL CreateTimer( TIMERCALLBACK Callback, PVOID Parameter, DWORD DueTime)
The Parameter above can't be ref_count_ptr. how do I? You can't in this case, you're stuck with what the API requires. Even though the API uses ugly macros for parameter types, you code will probably be easier to read if you don't.
Back to your original question, follow Alf's advice on variable names.
Considering the above CreateTimer binds an object (Parameter), to a function (Callback), why bother with the map?
I don't know " binds an object (Parameter), to a function (Callback) "
this is the only sentence about my question.
you mean "functor" ? VOID CALLBACK TimerRoutine(PVOID lpParam) { TimerParameter *p = (TimerParameter *)lpParam; Don't use C casts, in this case, use static_cast.
maybe reinterpret_cast -- Ian Collins.
Darwin Lalo wrote: BOOL CreateTimer( TIMERCALLBACK Callback, PVOID Parameter, DWORD DueTime)
The Parameter above can't be ref_count_ptr. how do I?
You can't in this case, you're stuck with what the API requires. Even though the API uses ugly macros for parameter types, you code will probably be easier to read if you don't.
Back to your original question, follow Alf's advice on variable names.
Considering the above CreateTimer binds an object (Parameter), to a function (Callback), why bother with the map?
I don't know " binds an object (Parameter), to a function (Callback) " this is the only sentence about my question. you mean "functor" ?
I was assuming that "TIMERCALLBACK" is a function pointer, but looking
at the function definition, maybe it isn't. What does "VOID CALLBACK"
mean? Is it a compiler extension? VOID CALLBACK TimerRoutine(PVOID lpParam) { TimerParameter *p = (TimerParameter *)lpParam;
Don't use C casts, in this case, use static_cast.
maybe reinterpret_cast
No, static_cast in this case (assuming PVOID expands to void*).
--
Ian Collins.
Ian Collins wrote: Darwin Lalo wrote: BOOL CreateTimer( TIMERCALLBACK Callback, PVOID Parameter, DWORD DueTime)
The Parameter above can't be ref_count_ptr. how do I?
You can't in this case, you're stuck with what the API requires. Even though the API uses ugly macros for parameter types, you code will probably be easier to read if you don't.
Back to your original question, follow Alf's advice on variable names.
Considering the above CreateTimer binds an object (Parameter), to a function (Callback), why bother with the map?
I don't know " binds an object (Parameter), to a function (Callback) " this is the only sentence about my question. you mean "functor" ?
I was assuming that "TIMERCALLBACK" is a function pointer,
yes
typedef void (* TIMERCALLBACK) (void *);
but looking at the function definition, maybe it isn't. What does "VOID CALLBACK" mean? Is it a compiler extension?
"VOID CALLBACK" is just " void " ,ok? VOID CALLBACK TimerRoutine(PVOID lpParam) { TimerParameter *p = (TimerParameter *)lpParam;
Don't use C casts, in this case, use static_cast.
maybe reinterpret_cast
No, static_cast in this case (assuming PVOID expands to void*).
-- Ian Collins.
Darwin Lalo wrote: I don't know " binds an object (Parameter), to a function (Callback) " this is the only sentence about my question. you mean "functor" ?
I was assuming that "TIMERCALLBACK" is a function pointer,
yes
typedef void (* TIMERCALLBACK) (void *);
but looking at the function definition, maybe it isn't. What does "VOID CALLBACK" mean? Is it a compiler extension?
"VOID CALLBACK" is just " void " ,ok?
OK,, so back to your original question, when you call CreateTimer, you
pass in a function pointer and a pointer to an object that will be
passed to the function when the timer expires.
So the logic you had in your original function was superfluous.
provided you pass in a unique object, it will be passed to you callback.
--
Ian Collins.
Darwin Lalo wrote: I have a lot of code like this:
VOID CALLBACK TimerRoutine(PVOID lpParam) { long nTaskid = (long)lpParam;
GObj *obj; if( mapThreadSafe.find( nTaskid, obj )) // mapThreadSafe is a hash_map, thread safe { obj->invoke(); } }
someone think it's inefficient and code like this:
It is indeed.
VOID CALLBACK TimerRoutine(PVOID lpParam) { GObj *obj = (GObj *)lpParam; obj->invoke(); obj->Release(); }
The above is the proper way of writing efficient code using C-style
callbacks.
I was confused.I think a hash_map is good enough and payable. it can avoid a lot of problem in a complex environment. what is your position?
First of all, a thread safe container is pretty much always a bad idea
because it does not work if you want to do more than one call in an
atomic sequence. This is why external locking is preferred.
Second, you don't really need a map here. Make it simple...
thank you for your straight answer
Maxim Yegorushkin wrote: Darwin Lalo wrote: I have a lot of code like this:
VOID CALLBACK TimerRoutine(PVOID lpParam) { long nTaskid = (long)lpParam;
GObj *obj; if( mapThreadSafe.find( nTaskid, obj )) // mapThreadSafe is a hash_map, thread safe { obj->invoke(); } }
someone think it's inefficient and code like this:
It is indeed.
VOID CALLBACK TimerRoutine(PVOID lpParam) { GObj *obj = (GObj *)lpParam; obj->invoke(); obj->Release(); }
The above is the proper way of writing efficient code using C-style callbacks.
I was confused.I think a hash_map is good enough and payable. it can avoid a lot of problem in a complex environment. what is your position?
First of all, a thread safe container is pretty much always a bad idea because it does not work if you want to do more than one call in an atomic sequence. This is why external locking is preferred.
Second, you don't really need a map here. Make it simple...
my map is like this:
template<class Key,class Type>
class simple_map{
axis::CriticalSection<true> _cs;
public:
bool find(const Key& _key,Type& _val){
axis::CriticalSectionLock lock(_cs);
std::map<Key,Type>::const_iterator _ite = _map.find(_key);
if( _ite == _map.end() )return false;
_val = _ite->second;
return true;
}
bool find(const Key& _key){
axis::CriticalSectionLock lock(_cs);
return ( _map.find(_key) != _map.end() );
}
template<class T,class P>
bool apply(T *_buddy, bool (T::*_fun)(Key key, Type type, P
parameter), P& _parameter){
axis::CriticalSectionLock lock(_cs);
_ite = _map.begin();
while( _ite != _map.end()){
Type& _tval = _ite->second;
const Key& _key = _ite->first;
++_ite;
if((_buddy->*_fun)(_key,_tval,_parameter))return true;
}
return false;
}
};
In my program, I have a lot of objects. I give each one a ID then put
their ptr in the map. Before using, I get it.
I am worry about the efficiency for the access is high frequency.
But I think the framework is clarity, and ID pass between function is
more clearer than object ptr.
Darwin Lalo wrote: In my program, I have a lot of objects. I give each one a ID then put their ptr in the map. Before using, I get it.
I am worry about the efficiency for the access is high frequency. But I think the framework is clarity, and ID pass between function is more clearer than object ptr.
But surely you can use the object pointer as a unique id?
--
Ian Collins.
Ian Collins wrote: Darwin Lalo wrote: In my program, I have a lot of objects. I give each one a ID then put their ptr in the map. Before using, I get it.
I am worry about the efficiency for the access is high frequency. But I think the framework is clarity, and ID pass between function is more clearer than object ptr. But surely you can use the object pointer as a unique id?
No. it is not a unique id.
int *p1 = new int;
delete p1;
int *p2 = new int;
(possibility) p1 == p2
But p1 is invaid. p2 is usable.
Yes, ref_count_ptr<int> can solve this problem.
But ref_count_ptr<int> is not a unique id(integral type).
-- Ian Collins.
Darwin Lalo wrote: Ian Collins wrote:
Darwin Lalo wrote:
In my program, I have a lot of objects. I give each one a ID then put their ptr in the map. Before using, I get it.
I am worry about the efficiency for the access is high frequency. But I think the framework is clarity, and ID pass between function is more clearer than object ptr.
But surely you can use the object pointer as a unique id?
No. it is not a unique id.
int *p1 = new int; delete p1; int *p2 = new int;
(possibility) p1 == p2
Ah, you didn't say objects are deleted from the map.
--
Ian Collins. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: scott ocamb |
last post by:
hello
I have implemented a solution using async methods. There is one async
method that can be invoked multiple times, ie there are multiple async
"threads" running at a time.
When these...
|
by: prettysmurfed |
last post by:
Hi all
I have a bit of a problem, the subject of this post is almost selfexplaing.
But here goes: Heres an example of the code I want to implement, its
all nice and simple, but the flaw is I...
|
by: Minfu Lu |
last post by:
I have a problem dealing with passing a function address to a COM callback. I
use this COM function for communicating to a hardware. My original project
was written in VB. I have converted it to...
|
by: ryan.mitchley |
last post by:
Hi all
I have a class (cPort) that is designed to receive objects and,
depending on the type, call a handler (callback) in any descendant of a
cProcessBlock class. Callback functions take a...
|
by: smmk25 |
last post by:
Before I state the problem, I just want to let the readers know, I am
knew to C++\CLI and interop so please forgive any newbie questions.
I have a huge C library which I want to be able to use in...
|
by: Evan Burkitt |
last post by:
Hi, all.
I have a Windows DLL that exports a number of functions. These functions
expect to receive a pointer to a callback function and an opaque void*
parameter. The callback functions are...
|
by: SQACPP |
last post by:
Hi,
I try to figure out how to use Callback procedure in a C++ form
project
The following code *work* perfectly on a console project
#include "Windows.h"
BOOL CALLBACK...
|
by: Angus |
last post by:
Hello
I am writing a library which will write data to a user defined callback
function. The function the user of my library will supply is:
int (*callbackfunction)(const char*);
In my...
|
by: Jef Driesen |
last post by:
I have a C DLL that I want to use from a C# project. The C header file
contains these declarations:
typedef void (*callback_t) (const unsigned char *data, unsigned int
size, void *userdata);...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: Aftab Ahmad |
last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below.
Dim IE As Object
Set IE =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
| |