多线程中的回调函数参数 [英] The callback function parameter in multithread

查看:599
本文介绍了多线程中的回调函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多这样的代码:


VOID CALLBACK TimerRoutine(PVOID lpParam)

{

long nTaskid =(长)lpParam;


GObj * obj;

if(mapThreadSafe.find(nTaskid,obj))// mapThreadSafe是

hash_map,线程安全

{

obj-> invoke();

}

}


有人认为它效率低,代码如下:


VOID CALLBACK TimerRoutine(PVOID lpParam)

{

GObj * obj =(GObj *)lpParam;

obj-> invoke();

obj->发布();

}


我很困惑。我认为hash_map足够好并且可以支付。

它可以避免在复杂的环境中很多问题。

你的职位是什么?

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:
* Darwin Lalo:
我有很多这样的代码:

VOID CALLBACK TimerRoutine(PVOID lpParam)
{
long nTaskid =(long)lpParam;

GObj * obj;
if(mapThreadSafe.find(nTaskid,obj))// mapThreadSafe是一个
hash_map,线程安全
{
obj-> invoke();
}
}
有人认为这样效率低,代码如下:

VOID CALLBACK TimerRoutine(PVOID lpParam)
{
GObj * obj =(GObj *)lpParam;
obj-> invoke();
obj-> ;释放();
}

我很困惑。我认为hash_map足够好并且可以支付。
它可以避免在复杂环境中出现很多问题。 />你的立场是什么?
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?




我认为摆脱虚拟指针是个好主意(隔离任何

必不可少的用在一些常见的小东西深处),C / /
样式演员(不安全的维护),误导性和一般

不可读的匈牙利前缀表示法(没有今天的优势),

不必要的宏,如VOID for void(和一般的宏),以及

手动冗余资源管理,如obj-> Release(替换为

智能指针,上面你有两个释放责任

问题和一个例外的不安全问题,两者都以更好的方式解决了。


我认为这将大大改善代码。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



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 - >发布不好。 ref_count_ptr很好。我同意。

但有时系统API需要一个PVOID参数,比如


BOOL CreateTimer(TIMERCALLBACK回调,PVOID参数,DWORD

到期时间)


上面的参数不能是ref_count_ptr。我怎么样?


喜欢这个?


? struct TimerParameter {

ref_count_ptr< GObj> obj;

};


CALL {

TimerParameter * p =新的TimerParameter;

CreateTimer (TimerRoutine,p,10);

}


VOID CALLBACK TimerRoutine(PVOID lpParam)

{

TimerParameter * p =(TimerParameter *)lpParam;

ref_count_ptr< GObj> obj = p-> obj;

删除p;


obj-> invoke();

}


谢谢你,也许我是新手。 :)

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写道:
Darwin Lalo wrote:
obj->发布不好。 ref_count_ptr很好。我同意。
但有时System API需要一个PVOID参数,比如

你应该在你的回复中引用上下文,而不是每个人都使用google!

BOOL CreateTimer(TIMERCALLBACK Callback) ,PVOID参数,DWORD
DueTime)

上面的参数不能是ref_count_ptr。我该怎么办?

在这种情况下你不能坚持API所需要的东西。即使

虽然API使用丑陋的宏作为参数类型,但如果你不这样做,代码将会更容易阅读。


回到你原来的问题,按照Alf对变量名的建议。


考虑到上面的CreateTimer将一个对象(参数)绑定到一个

函数(回调),为什么要打扰地图?

VOID CALLBACK TimerRoutine(PVOID lpParam)
{/> TimerParameter * p =(TimerParameter *)lpParam;
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;




不要使用C强制转换,在这种情况下,请使用static_cast。


-

Ian Collins。



Don''t use C casts, in this case, use static_cast.

--
Ian Collins.


这篇关于多线程中的回调函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆