多线程和等待而不阻塞 [英] Multithreading and Waiting without Blocking

查看:93
本文介绍了多线程和等待而不阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下情形:


数据包是从串口发出的,预计返回包的时间很短。

。 br />

发送数据包的应用程序需要在收到返回数据包后立即发送另一个数据包

。它需要等待返回数据包,

但是如果没有收到则超时。


在交换数据包时,应用程序需要响应。


为了最大化吞吐量,是否应该在单独的线程上发送数据包?

如何等待线程完成(不阻塞)

命令发送后续数据包?循环使用DoEvents会导致CPU%上涨,然后睡眠(x)会导致数据包之间出现过度延迟。


TIA


Charles

解决方案

Charles,


取决于方法(RS232控件,API -calls,...)你使用

与RS232通信,你可以利用一个事件或回调

每当返回数据进来时。


问候,

jan


" Charles Law" < BL *** @ nowhere.com>在留言中写道

news:e6 **************** @ TK2MSFTNGP12.phx.gbl ...

考虑以下内容场景:

数据包是从串口发出的,回程数据包预计会在短时间内发送到
a。

发送数据包的应用程序需要发送一旦收到返回数据包,就会发送另一个数据包。它需要等待返回的
数据包,但如果没有收到则会超时。

在交换数据包时,应用程序需要响应。

为了最大化吞吐量,数据包应该在单独的线程上发送吗?
应用程序如何等待线程完成(不阻塞)
以便发送后续数据包?循环使用DoEvent会导致CPU%上升,而Sleep(x)会在数据包之间引入过多的延迟。

TIA

Charles


嗨Charles,


我这样做只是让subthread引发一个公共事件并设置一个处理程序

用于启动帖子的程序。


我该怎么办?


Cor
< blockquote class =post_quotes>请考虑以下情况:

数据包是从串口发出的,返回数据包预计会在短时间内发送到
a。
数据包,但如果没有收到则会超时。

在交换数据包时,应用程序需要响应。

为了最大化吞吐量,数据包应该在单独的线程上发送吗?
应用程序如何等待线程完成(不阻塞)
以便发送后续数据包?循环使用DoEvent会导致CPU%上升,而Sleep(x)会在数据包之间引入过多的延迟。

TIA

Charles


你好Jan


收到数据时我有一个事件,我做了我的包
那里有
检测。此事件已经在一个单独的线程上,所以一旦收到一个

完整数据包,我只需设置一个标志来表示到达。

我的主要发送和接收线程需求等待这个标志,但是我不希望
想要使用过多的cpu时间来有效地轮询标志直到它被设置。

这就是为什么我想知道一个等待另一个线程,或类似

那样的东西。


Charles

" Jan" < MS **** @ tss.be>在消息中写道

news:%2 **************** @ TK2MSFTNGP11.phx.gbl ...

Charles,

根据您用来与RS232通信的方法(RS232控制,API调用......),您可以使用事件或回调
每当返回数据进来时。

问候,
jan

Charles Law < BL *** @ nowhere.com>在消息中写道
新闻:e6 **************** @ TK2MSFTNGP12.phx.gbl ...

请考虑以下情况:

数据包是从串口发出的,返回包是
预期

a

短时间后。

发送数据包的应用程序需要在收到返回数据包后立即发送另一个数据包



。它需要等待返回


数据包,

但是如果没有收到则需要超时。

虽然正在交换数据包,但应用程序需要响应是

为了最大化吞吐量,数据包应该在一个单独的线程上发送吗?


如何

应用程序将等待线程在

中完成(不阻塞)


以便发送后续数据包?循环使用DoEvent会导致CPU%上升,而Sleep(x)会在数据包之间引入过多的延迟。

TIA

Charles




Consider the following scenario:

A data packet is sent out of a serial port and a return packet is expected a
short time later.

The application sending the packet needs to send another packet as soon as
the return packet has been received. It needs to wait for the return packet,
but time out if one is not received.

Whilst packets are being exchanged the application needs to be responsive.

To maximise throughput, should the packets be sent on a separate thread? How
would the application wait for the thread to complete (without blocking) in
order to send subsequent packets? Looping with DoEvents causes CPU % to
shoot up, and Sleep(x) introduces excessive delays between packets.

TIA

Charles

解决方案

Charles,

Dependent on the method (an RS232 control, API-calls, ...) you use to
communicate with RS232, you might make use of an event or a callback
whenever return-data comes in.

Regards,
jan

"Charles Law" <bl***@nowhere.com> wrote in message
news:e6****************@TK2MSFTNGP12.phx.gbl...

Consider the following scenario:

A data packet is sent out of a serial port and a return packet is expected a short time later.

The application sending the packet needs to send another packet as soon as
the return packet has been received. It needs to wait for the return packet, but time out if one is not received.

Whilst packets are being exchanged the application needs to be responsive.

To maximise throughput, should the packets be sent on a separate thread? How would the application wait for the thread to complete (without blocking) in order to send subsequent packets? Looping with DoEvents causes CPU % to
shoot up, and Sleep(x) introduces excessive delays between packets.

TIA

Charles



Hi Charles,

I do it by just let the subthread raise an public event and set a handler
for that in the program that starts the thread.

What do I mis?

Cor

Consider the following scenario:

A data packet is sent out of a serial port and a return packet is expected a short time later.

The application sending the packet needs to send another packet as soon as
the return packet has been received. It needs to wait for the return packet, but time out if one is not received.

Whilst packets are being exchanged the application needs to be responsive.

To maximise throughput, should the packets be sent on a separate thread? How would the application wait for the thread to complete (without blocking) in order to send subsequent packets? Looping with DoEvents causes CPU % to
shoot up, and Sleep(x) introduces excessive delays between packets.

TIA

Charles



Hi Jan

I have an event that is raised when data is received, and I do my packet
detection there. This event is already on a separate thread, so once a
complete packet has been received I just set a flag to indicate the arrival.
My main send and receive thread needs to wait for this flag, but I don''t
want to use excessive cpu time effectively polling the flag until it is set.
That is why I wondered about a wait on another thread, or something like
that.

Charles
"Jan" <ms****@tss.be> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

Charles,

Dependent on the method (an RS232 control, API-calls, ...) you use to
communicate with RS232, you might make use of an event or a callback
whenever return-data comes in.

Regards,
jan

"Charles Law" <bl***@nowhere.com> wrote in message
news:e6****************@TK2MSFTNGP12.phx.gbl...

Consider the following scenario:

A data packet is sent out of a serial port and a return packet is expected

a

short time later.

The application sending the packet needs to send another packet as soon


as the return packet has been received. It needs to wait for the return


packet,

but time out if one is not received.

Whilst packets are being exchanged the application needs to be responsive.
To maximise throughput, should the packets be sent on a separate thread?


How

would the application wait for the thread to complete (without blocking)


in

order to send subsequent packets? Looping with DoEvents causes CPU % to
shoot up, and Sleep(x) introduces excessive delays between packets.

TIA

Charles




这篇关于多线程和等待而不阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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