在C中的不同线程中发送回调 [英] Send a callback in different threads in C
本文介绍了在C中的不同线程中发送回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以不同的线程发送我的回调是否可能?
我尝试过:
我不断向其他应用程序发送回调
typedef struct
{
char * Domain;
char * Uri;
UINT32 IpAddr;
UINT32端口;
int 方向; // 0:-InBound,1:-Outbound
} PACKETINFO,* PPACKETINFO;
typedef int (CALLBACK * ProgressCallback)(PACKETINFO * info);
Dowork(ProgressCallback funcaddr)
{
while ( true )
{
if (funcaddr!= 0 )
{
// callback send here
res = funcaddr(PINFO);
}
}
}
解决方案
这不是一个好主意,因为你需要转移内存和控制到另一个可能会暂停或退出一段时间的线程。
所以我会将带有一些传递函数的数据(在线程1中)安排到一些附加的存储器中数据(在线程2的内存中)。比线程2可以获取数据并对其进行处理。
处理线程2终止时的场景,因此将线程1中的函数指针设置为NULL。 / blockquote>
是的,你当然可以这样做。
我会写Dowork
以这种方式运作
Dowork(ProgressCallback funcaddr)
{
if (funcaddr!= 0 )
{
while ( true )
{
res = funcaddr(pinfo);
}
}
}无论如何:
res
已定义?- 同样,
pinfo
在哪里定义?- 为什么要迭代一个无休止的循环,简单地丢弃回调返回值?
i want to send my callbacks in different threads is it possible ?
What I have tried:
iam continuously sending callbacks to other application
typedef struct { char *Domain; char *Uri; UINT32 IpAddr; UINT32 Port; int Direction; //0:-InBound,1:-Outbound }PACKETINFO, *PPACKETINFO; typedef int(CALLBACK *ProgressCallback)(PACKETINFO *info); Dowork(ProgressCallback funcaddr) { while(true) { if(funcaddr !=0 ) { //callback send here res = funcaddr(pinfo); } } }解决方案It isnt a good idea, because you need to transfer memory and control to another thread which may pause or exit some time.
So I would schedule the data with some transfer function (in thread 1) into some storage which appends the data (in the memory of thread 2). Than the thread 2 can fetch the data and work on it.
Handle the scenario when thread 2 is terminated and so set the function pointer in thread 1 to NULL.
Yes, you can do it, of course.
I would write theDowork
function this way
Dowork(ProgressCallback funcaddr) { if ( funcaddr != 0) { while(true) { res = funcaddr(pinfo); } } }Anyway:
- Where is
res
defined?- As well, where is
pinfo
defined?- Why are you iterating on a neverending loop, simply discarding the callback return value?
这篇关于在C中的不同线程中发送回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文