java 线程 回调函数,多线程中的回调函数参数

博客讨论了在回调函数中使用哈希映射与直接转换指针的效率和安全性问题。作者指出,虽然哈希映射提供了一定的复杂环境下的安全性,但可能导致效率下降。另一种方案是通过智能指针进行资源管理,以避免手动释放导致的问题。同时,面对需要接受void指针的系统API,可以创建一个结构体包装智能指针。讨论中建议使用静态类型转换,避免C风格的强制转换,并强调变量命名的重要性。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值