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?
解决方案
* 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 obj;
};
CALL {
TimerParameter *p = new TimerParameter;
CreateTimer(TimerRoutine, p, 10);
}
VOID CALLBACK TimerRoutine(PVOID lpParam)
{
TimerParameter *p = (TimerParameter *)lpParam;
ref_count_ptr 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.