使用select唤醒睡眠中的线程

4 篇文章 0 订阅
1 篇文章 0 订阅
有时我们需要使用多线程每隔一定时间自动去完成一些特定的工作,但有时我们也需要线程在某种情况发生时,从睡眠中唤醒并立即去完成工作,这时
sleep就显得无能为力了,但这正是select登场的时候。
这里我们就不介绍select的用法了,如果您还不知道select怎么用的,您可以去找本书看看,悄悄的告诉你,可以去《unix网络编程卷1:套接字联网api》
这本上取经。

select原型:
int select(int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds, struct timeval *timeout);
我们都知道select返回的条件是:readfds上有描述符可读,
writefds上有描述符可写,exceptfd有异常发生,或者timeout超
时,当然首先这些参数不能被设置为NULL;有timeout就有定时功能,其他的就可以当中断用来完成唤醒线程。我们使用管道的读端描述符来完成可读条件的测试,代码如下:

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdarg.h>                 
#include <syslog.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <netdb.h>
#include <pthread.h>
int pipefd[2];
void *mythread(void *);
int main(void)
{
    char str[1024];
    pthread_t thread;
    int n = 0;
    if(pipe(pipefd) < 0)
    {
        printf("pipe error: %s\n", strerror(errno));
        return -1;
    }
    if(pthread_create(&thread, NULL, mythread, NULL) != 0)
    {
        printf("create pthread error\n");
        exit(1);
    }   
    while((fgets(str, sizeof(str) - 1, stdin)) != NULL)
    {
        n = strlen(str) + 1;
        if(write(pipefd[1], str, n) != n)
        {
            printf("write error: %s\n", strerror(errno));
            exit(1);
        }
    }
    close(pipefd[1]);
    pthread_join(thread, NULL);
    return 0;
}
void *mythread(void *arg)
{
    fd_set rdfs;
    struct timeval timeout;
    int select_ret;
    char buf[1024];
    int n;
    for(;;)
    {
        FD_ZERO(&rdfs);
        FD_SET(pipefd[0], &rdfs);
        timeout.tv_sec = 10;
        timeout.tv_usec = 0;
        if((select_ret = select(pipefd[1] + 1, &rdfs, NULL, NULL, &timeout)) < 0)
        {
            printf("select error: %s\n", strerror(errno));
            exit(1);
        }
        else if(select_ret == 0)
        {
            printf("timeout\n");
            continue;
        }
        else if(select_ret == 1)
        {
            if(FD_ISSET(pipefd[0], &rdfs))
            {
                if((n = read(pipefd[0], buf, sizeof(buf) - 1)) > 0)
                {
                    printf("thread get data: %s", buf);
                    continue;
                }
                else if(n == 0)
                {
                    close(pipefd[0]);
                    printf("read EOF, thread will be over\n");
                    return NULL;
                }
                else
                {
                    printf("read error\n");
                }
            }
        }
        else
        {
            printf("select unknow error\n");
        }
    }
}

使用方法:编译成功后(需要加-lpthread),每个十秒会打印timeout,但如果您在键盘上输入了字符并按了回车,那么线程会立即打印您输入的内容并告知您是线程打印的。
如(centos下测试):
[root@localhost pipe]# ./select_sleep
timeout
test
thread get data: test
timeout
timeout
timeout
timeout

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值