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

The callback function parameter in multithread

P: n/a
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?

Apr 24 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
* 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?
Apr 24 '06 #2

P: n/a
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. :)

Apr 24 '06 #3

P: n/a
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.
Apr 24 '06 #4

P: n/a

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.


Apr 24 '06 #5

P: n/a
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.
Apr 24 '06 #6

P: n/a

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.


Apr 24 '06 #7

P: n/a
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.
Apr 24 '06 #8

P: n/a

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...

Apr 24 '06 #9

P: n/a
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.

Apr 24 '06 #10

P: n/a
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.
Apr 24 '06 #11

P: n/a

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.


Apr 24 '06 #12

P: n/a
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.
Apr 24 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.