从插座突破选择 [英] breaking out from socket select

查看:109
本文介绍了从插座突破选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,基本上调用此每隔几秒钟(超时后):

I have a loop which basically calls this every few seconds (after the timeout):

 while(true){

    if(finished)
       return;

    switch(select(FD_SETSIZE, &readfds, 0, 0, &tv)){
        case SOCKET_ERROR : report bad stuff etc; return;
        default : break;
    }

    // do stuff with the incoming connection
 }

因此​​,基本上每隔几秒(这是由电视指定)为,它重新激活监听。

So basically for every few seconds (which is specified by tv), it reactivates the listening.

这是在线程B(而不是主线程)运行。有些时候,我想从线程A(主线程)立即停止这种受体循环,但好像我不得不等待,直到时间间隔结束。

This is run on thread B (not a main thread). There are times when I want to end this acceptor loop immediately from thread A (main thread), but seems like I have to wait until the time interval finishes..

有没有办法破坏从另一个线程的选择功能,使线程B可以立即退出?

Is there a way to disrupt the select function from another thread so thread B can quit instantly?

推荐答案

最简单的方法可能是使用管(2)以创建一个管道,并添加读端到 readfds 。当其他线程要中断选择()只写它一个字节,然后使用它之后。

The easiest way is probably to use pipe(2) to create a pipe and add the read end to readfds. When the other thread wants to interrupt the select() just write a byte to it, then consume it afterward.

这篇关于从插座突破选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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