-1

There is a API for me to get some data,such as:

int API_func(pointer * data)

the return value is used to judge whether the function is done, and the data will be writen to the pointer address; but if something wrong, the API_func may not get return ,The program may get stuck,so I need a timer to calculate the timer when call the API_func,and once the time over 30s ,I need to send a signal to GUI,so how can I do? I am a green hand ,so I want know how the Master do ? My method is:

Thread_1: main Thread ,call API here,

Thread_2: timer Thread,include a timer function

when Thread_1 call API,send a msg(FIFO) to active the timer function at timer Thread,if the API cant return successed ,the timer at the Thread_2 will overtime ,then send a msg to GUI ?

is any better method ? thank a lot! sorry,i am green hand in stackoverflow,so i dont how to express what I mean: fake code:

 Thread_1:
    set_timer_flag_on(FILE *time_flag_1)
    API_func()
    Thread_2:
    if (1==check_timer_flag(FILE *time_flag_1))
    {
        timer_func(set_over_time);
    }
jiapeng.ye
  • 13
  • 4
  • So you have a solution, which only lacks in not covering the time case. Please show that code; similar to a [mcve], apart from the fact that the error case is hard to demonstrate. I.e. show the code which handles the normal cases. Make a simplified example, using e.g. pseudo code `/* this is where an endless loop might occur */`. – Yunnosch Sep 25 '18 at 08:17
  • i add some fake code ,maybe help to understand what i mean.and i want know is the method,not how to coding,thank you very much. – jiapeng.ye Sep 25 '18 at 08:46

1 Answers1

0

you can call int API_func(pointer* data) in a separate thread and signal the main thread by a condition variable. For int API_func(pointer* data) you write a wrapper. an example is in How to implement timeout for function in c++

ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • thank you very much! I am a green hand ,and the way above is design by myself ,without anybody's help,so I afraid my way may be fool 。accoding to your advice ,use multi-thread is a normal way,i know how to do it ,thank you ! – jiapeng.ye Sep 25 '18 at 08:52