commit 346b276c0a78e41c9f0896da9cc11d46a578eb68 Author: impressionyang Date: Sun Jan 24 14:54:05 2021 +0800 first initial update diff --git a/README.md b/README.md new file mode 100644 index 0000000..680137a --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# linux cpp timeout package class + +it is use to check timeout in a task in linux c plus plus + +## how to use + + 1. `include "ctimeout.h"` + 2. define object `CTimeOut timeout` + 3. set the timeout whit time `timeout.set_timeout_msec(1000)` + 4. then you can check if the time in your task is timeout you can use `timeout.is_timeout()`to get a bool return to confirm it diff --git a/timeout_test/Makefile b/timeout_test/Makefile new file mode 100644 index 0000000..9c1b09c --- /dev/null +++ b/timeout_test/Makefile @@ -0,0 +1,13 @@ +object = main.o ctimeout.o +run_target = timeout_test +LFLAG = + +main : $(object) + g++ -o $(run_target) $(object) $(LFLAG) + +main.o : main.cpp +ctimeout.o : ctimeout.h ctimeout.cpp + +.PHONY : clean +clean: + rm $(object) \ No newline at end of file diff --git a/timeout_test/ctimeout.cpp b/timeout_test/ctimeout.cpp new file mode 100644 index 0000000..8a5b4d5 --- /dev/null +++ b/timeout_test/ctimeout.cpp @@ -0,0 +1,43 @@ +#include "ctimeout.h" + +int CTimeOut::set_timeout_msec(int msec) +{ + if (msec <= 0) { + return 1; + } + + gettimeofday(&timeout_set, NULL); + + // 分解设置的毫秒数 + long usec2add = (msec & 1000) * 1000; + int sec2add = (msec / 1000); + + // 算出加上超时时间后的微秒数和秒数 + long real_usec = (timeout_set.tv_usec + usec2add) % 1000000; + int real_sec = ((timeout_set.tv_usec + usec2add) / 1000000) + sec2add + timeout_set.tv_sec; + + // 设置超时的时间 + timeout_set.tv_sec = real_sec; + timeout_set.tv_usec = real_usec; + + return 0; +} + +int CTimeOut::set_timeout_sec(int sec) +{ + if (sec <= 0) { + return 1; + } + + gettimeofday(&timeout_set, NULL); + + timeout_set.tv_sec += sec; + + return 0; +} + +bool CTimeOut::is_timeout() +{ + gettimeofday(&timeout_now, NULL); + return timeout_now.tv_sec >= timeout_set.tv_sec && timeout_now.tv_usec >= timeout_set.tv_usec; +} \ No newline at end of file diff --git a/timeout_test/ctimeout.h b/timeout_test/ctimeout.h new file mode 100644 index 0000000..28e56e0 --- /dev/null +++ b/timeout_test/ctimeout.h @@ -0,0 +1,19 @@ +#ifndef _TIMEOUT_H_ +#define _TIMEOUT_H_ + +#include +#include + +class CTimeOut +{ +public: + int set_timeout_msec(int msec); + int set_timeout_sec(int sec); + bool is_timeout(); + +private: + timeval timeout_set; + timeval timeout_now; +}; + +#endif \ No newline at end of file diff --git a/timeout_test/main.cpp b/timeout_test/main.cpp new file mode 100644 index 0000000..94418ff --- /dev/null +++ b/timeout_test/main.cpp @@ -0,0 +1,20 @@ +#include "ctimeout.h" +#include + +using namespace std; + +int main(int argc, char **argv) +{ + CTimeOut timeout; + int i = 0; + + timeout.set_timeout_msec(500); + while (1) { + if (timeout.is_timeout()) { + cout << "i = " << i++ << endl; + timeout.set_timeout_msec(500); + } + } + + return 0; +} \ No newline at end of file diff --git a/timeout_test/timeout_test b/timeout_test/timeout_test new file mode 100755 index 0000000..a46a122 Binary files /dev/null and b/timeout_test/timeout_test differ