线程数 c++ 之间的事件

发布于 2024-12-25 00:04:18 字数 160 浏览 1 评论 0原文

如果我希望线程 A 接收其数据包并处理它们,并且某个线程 B 可以在某个时刻更改套接字的 IP 地址。

所以这里需要做的事情是: 线程 A 必须开始侦听新 IP,线程 B 必须以某种方式告诉线程 A。

有什么建议吗?请这对我来说非常重要。

If I want thread A to receive its packets and process them, and some thread B can at some point change the socket's IP address.

So the thing that need to be done here is:
Thread A must start on listening on the new IP and thread B must somehow tell that to thread A.

Any suggestions? please it is very important for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

习ぎ惯性依靠 2025-01-01 00:04:18

您可能需要一个工作流程,例如:

// thread A
// receive packets on initial address
WaitForSingleObject(event, INFINITE); // wait for address change
// receive packets on the new address

// thread B
// do some work
// change IP address
SetEvent(event); // signal A to start receiving on the new address

You probably want a workflow such as:

// thread A
// receive packets on initial address
WaitForSingleObject(event, INFINITE); // wait for address change
// receive packets on the new address

and

// thread B
// do some work
// change IP address
SetEvent(event); // signal A to start receiving on the new address
一页 2025-01-01 00:04:18

事实上,我会让我的评论成为一个答案:

创建/启动另一个线程A实例,向其传递新的IP/端口。在旧线程 A 中设置一些标志,告诉它退出并在它醒来时进行清理。

如果你想让事情进展顺利一点,或者你经常执行这个“更改套接字”操作,请关闭旧线程 A 上的侦听套接字 - 它的accept() 调用将因某些异常或错误而失败,然后旧线程 A 可以清理自身并死亡。

In fact, I'll make my comment an answer:

Create/start another thread A instance, passing it the new IP/port. Set some flag in the old thread A, telling to exit itself and clean up if and when it ever wakes up.

If you want to gee things along a bit, or you do this 'change socket' operation frequently, close the listening socket on the old thread A - it's accept() call will fail with some exception or error and the old thread A can then clean up itself and die.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文