博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
how to set thread timeout time
阅读量:7127 次
发布时间:2019-06-28

本文共 2124 字,大约阅读时间需要 7 分钟。

  I wanted to download a file from Internet while I find the wget may stuck when the network is not good enough .

"wget" command  accepts -T / --timeout , however, it doesn't work right as I tried .

 

  So  I create a thread to handle this job , using pthread_cond_timedwait .

  the following program run like this : main thread create a sub_thread call thread_download in order to handle the job of download a file .  If thread_download finished in time ,it signal the main thread to stop waiting and continue somes tasks. If not, (While time's up,) it cancel the thread_download.

 

 

 

#include <stdio.h> 

#include <stdlib.h>

#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
pthread_mutex_t count_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t count_cond = PTHREAD_COND_INITIALIZER;
unsigned int count = 0;

/*if it = 1 then it means download job finished in time*/

void *thread_download(void *arg)
{
    
    system("wget ftp://192.168.100.3:22/ftpdir/1.rmvb -O 1.rmvb ");
    count += 1;
    pthread_mutex_lock(&count_lock);
    pthread_cond_signal(&count_cond);
    pthread_mutex_unlock(&count_lock);
    
    pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
    struct timespec m_time;
    pthread_t reader2;
    int res = 0;
    int msec; /*time out millionsec  */

pasting
    
if (argc < 
2) exit(
1);
    msec = atoi(argv[
1]);
    
    pthread_create(&reader2, NULL, thread_download, NULL);
    
/*
 get the current time 
*/ 
    
struct timeval now; 
    gettimeofday(&now, NULL); 
    
    
/*
 add the offset to get timeout value 
*/ 
    m_time.tv_nsec = now.tv_usec * 
1000 + (msec % 
1000) * 
1000000
    m_time.tv_sec = now.tv_sec + msec / 
1000;
    pthread_mutex_lock(&count_lock);
    
while(count == 
0 && res != ETIMEDOUT)
        res = pthread_cond_timedwait(&count_cond, &count_lock, (
const 
struct timespec *)&m_time);
    
    pthread_mutex_unlock(&count_lock);
    printf(
"
res:%d\n
", res);
   
   
if(res==ETIMEDOUT)
   {
        printf(
"
\n\n********* timeout **********\n\n
");
//
    if (pthread_kill(reader2,0)!=0)
    {
        printf(
"
\n********* pthread_cancel **********\n
");
        pthread_cancel(reader2);
    }
   }
   
else
   {
        printf(
"
 finish in time ,res = %d, count =%d\n
", res , count);
   }
    pthread_join(reader2, NULL);
    
return 
0;
}

转载地址:http://kbeel.baihongyu.com/

你可能感兴趣的文章
简单shell脚本监控网站页面
查看>>
zookeeper 安装使用教程
查看>>
asp.net core系列
查看>>
redis备份实操
查看>>
浅谈Oracle SQL trace
查看>>
MySQL基础备忘(1)
查看>>
技术公开课:SQL Server 高可用性解决方案概述(下)
查看>>
Skype for Business Server 2015-04-前端服务器-5-创建DNS记录
查看>>
Lync Server外部访问系列PART4:部署反向代理
查看>>
DPM2012系列之六:在Win7上安装DPM远程管理控制台
查看>>
PowerShell 学习笔记——运行命令
查看>>
联想K系引领智能电视潮流,Android 4.0将成行业标配
查看>>
Cocos2d-x Touch事件处理机制(better)
查看>>
那些年借“云”出海的日子
查看>>
Lync Server 2010的部署系列(四) outlook无法加入联机会议
查看>>
网易星球手机挖矿,仿公信宝不要太明显!伪区块链搜集用户隐私?
查看>>
第一次获得Microsoft MVP应该做的事
查看>>
java:Eclipse:Juno:设置workspace路径
查看>>
MySQL5.6更人性化修改redo log事务日志文件大小
查看>>
Office 365系列之十三:Office 365管理员角色
查看>>