线程数 c++ 之间的事件
如果我希望线程 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要一个工作流程,例如:
和
You probably want a workflow such as:
and
事实上,我会让我的评论成为一个答案:
创建/启动另一个线程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.