【问题标题】:make pthread exit within fixed time使pthread在固定时间内退出
【发布时间】:2016-12-17 12:54:01
【问题描述】:

这是我的情况

global var n;
global var result;
void* solve_for_n(void *arguments)
{    does_something;
     put value calculated in result;
}
function main
{    for(n=1;n<30000;n++)
     {   set value of n;
         create pthread with address of function solve_for_n;
         wait for reply from pthread;
         if 5 seconds have passed tell pthread to exit;
     }
}

n 是 main 设置的变量。
solve_for_n 设置类似的结果
我想给 pthread 最多 5 秒的时间来解决,否则它应该退出。
还有更多 n 值在排队等待。

现在,这是我的代码

#include<stdio.h>
#include<pthread.h>
#include<sys/time.h>
#define micro_second_multiplier 1000000

float interval;
unsigned long long int m;
unsigned int done,length,n;
int bye;
pthread_mutex_t timeisup;
void * solveforn(void *arguments)
{   
    unsigned long long int multiplicant,sum,quotient,divisor,product,multiplicantincremented,multiplicantdivisor;
    unsigned int flag,lenofn,maxnumofdigitinproduct,lenmultiplicant,digit;
    struct timeval begin, end;

    gettimeofday(&begin, NULL);
    printf("\nPthread begins");
    lenofn=0;
    divisor=10ll;
    do
    {   quotient=((unsigned long long)n)/divisor;
        divisor=divisor*(10ll);
        lenofn++;
    }while(quotient!=(0ll));
for(multiplicant=multiplicantincremented=1ll,flag=0,lenmultiplicant=1,multiplicantdivisor=10ll;flag!=1;multiplicant++,multiplicantincremented++)
    {   if((multiplicant%(2ll)==(0ll))&&(n%5==0))
        {   multiplicant++;
            if(multiplicantincremented==multiplicantdivisor)
            {   lenmultiplicant++;
                multiplicantdivisor=multiplicantdivisor*(10ll);
            }
            multiplicantincremented++;
        }
        else if(multiplicantincremented==multiplicantdivisor)
            {   lenmultiplicant++;
                multiplicantdivisor=multiplicantdivisor*(10ll);
            }
        maxnumofdigitinproduct=lenmultiplicant+lenofn;
        m=((unsigned long long)n)*multiplicant;
        length=0;
        sum=0ll;
        product=1ll;
        divisor=10ll;
        do
        {   quotient=m/divisor;
            length++;
            digit=(m%divisor)/(divisor/(10ll));
            divisor=divisor*(10ll);
            if(digit==0)
            {   product=0ll;
                break;
            }
            else
            {   
                sum=sum+((unsigned long long)digit);
                product=product*((unsigned long long)digit);
                if( (sum<product) && (sum+((unsigned long long)maxnumofdigitinproduct)-((unsigned long long)length)<product) )
                {
                    product=0ll;
                    break;
                }
            }
        }while(quotient!=(0ll));
        if((sum>=product)&&(product!=(0ll)))
            flag=1;
        pthread_mutex_lock(&timeisup);
        if(bye==1)
        {   pthread_mutex_unlock(&timeisup);
            pthread_exit(NULL);
        }
        else
            pthread_mutex_unlock(&timeisup);
    }
    gettimeofday(&end, NULL);
    interval=(float)(end.tv_sec*micro_second_multiplier+end.tv_usec - begin.tv_sec*micro_second_multiplier-begin.tv_usec);
    done=1;
    printf("\nn=%d m=%llu length=%d %.f millisecond = %.f second done=%d",n,m,length,interval,interval/1000,done);
}

int main(void)
{   pthread_t sa;
    int err_pthread,count;
    FILE *fi;
    fi=fopen("log.txt","a+");
    for(n=1;n<=30000;n++)
    {   if(n%10==0)
            n++;
        printf("\nTrying n=%d",n);
        done=0;
        bye=0;
        err_pthread=pthread_create(&sa,NULL,&solveforn,NULL);
        if(err_pthread!=0)
                {   printf("\nError while creating pthread for n=%d",n);
        }
        else
        {   printf("\npthread created");
            pthread_join(sa,NULL);
            count=0;
            do
            {   sleep(1);
                printf("\n1 second gone");
                count++;
            }while((done!=1) && (count<5));
            if(done==1)
            {   fprintf(fi,"\nn=%d m=%llu length=%d %.f millisecond = %.f second",n,m,length,interval,interval/1000);
                printf("\nThread finished in < 5 seconds");
            }
            else
            {   fprintf(fi,"\nTIME LIMIT EXCEEDED IN N=%d",n);
                printf("\nTIME LIMIT EXCEEDED IN N=%d",n);
                pthread_mutex_lock(&timeisup);
                bye=1;
                pthread_mutex_unlock(&timeisup);
            }
        }
    }
    fclose(fi);
}

需要注意的点是
(1)mutex timeisup 锁定,然后在 main 和 solveforn 之间传递数据。
希望-我能够解释我的情况。
问题是 -
输出如下
尝试 n=1
pthread 创建
Pthread 开始
n=1 m=1 长度=1 28 毫秒 = 0 秒完成=1
1秒过去了
线程在 尝试 n=2
pthread 创建
什么都没有


第二个“pthread 开始”没有出现。 谢谢。

【问题讨论】:

    标签: pthreads


    【解决方案1】:

    有一些解决方案,如 pthread_cancel 或 pthread_kill,但它们都不是你想要的。你必须实现处理程序和...

    杀死由 pthread 创建的线程没有安全的方法(您询问了退出),您应该在线程函数中实现退出逻辑。 您也可以等待 5 秒,如果所有线程都退出(使用适当的互斥体更改标志),您可以离开。

    For pthread, How to kill child thread from the main thread

    cancelling or killing a pthread

    锁定/解锁调用也存在不对称性,在您的代码中,您已锁定 pthread_mutex_lock 并检查了 bye==1,如果是互斥锁将被解锁,但如果再见!= 互斥锁永远不会被解锁并且在下一次迭代中,您将被不正确的 mutex_lock 阻止。

    【讨论】:

    • 感谢您的回复。在我的代码中 - 我有一个全局变量 'bye' 在线程创建之前设置为零。 5 秒后,如果线程尚未完成其工作,则 main 锁定 mutex 'time is up' 并将 bye 设置为 1 。线程在处理的每个循环中检查 bye 值。想不通 - 为什么代码没有运行一点?
    • @user1371666 看起来没问题,您必须发布较小版本的代码
    • 编辑了我的代码和相应的输出。如果您使用 gcc,请将其另存为 something.c $gcc something.c -lpthread

    • 编辑了我的代码和相应的输出。
      如果你使用gcc
      那么请将它保存为something.c
      $gcc something.c -lpthread
      $./a.out
      自己看看。
      谢谢。
    • 对不起上面的格式。似乎 cmets 中的回车是一个问题。
    猜你喜欢
    • 2010-10-09
    • 2012-03-25
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多