{"id":15402166,"url":"https://github.com/liby99/timeout","last_synced_at":"2025-07-11T18:39:19.750Z","repository":{"id":90736013,"uuid":"290694129","full_name":"Liby99/timeout","owner":"Liby99","description":null,"archived":false,"fork":false,"pushed_at":"2020-08-27T06:54:58.000Z","size":4,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-21T10:51:05.457Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Liby99.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-27T06:24:17.000Z","updated_at":"2020-08-27T06:55:00.000Z","dependencies_parsed_at":null,"dependency_job_id":"0e104219-b025-4a7f-aa73-b326d195dd8c","html_url":"https://github.com/Liby99/timeout","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liby99%2Ftimeout","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liby99%2Ftimeout/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liby99%2Ftimeout/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Liby99%2Ftimeout/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Liby99","download_url":"https://codeload.github.com/Liby99/timeout/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243616988,"owners_count":20319989,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T16:01:20.454Z","updated_at":"2025-03-14T17:25:15.185Z","avatar_url":"https://github.com/Liby99.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Timeout function in C++\n\nProvide a task (in the form of a function) and a time. It either completes\nthe task within the budget or throw a timeout error. The timeout function itself\nwill return whatever the task function returns, and you should provide whatever\nthe task function requires as function arguments.\n\nInternally, it creates another thread and uses mutex + conditional variables.\nRefer to [this stackoverflow question](https://stackoverflow.com/questions/40550730/how-to-implement-timeout-for-function-in-c) for more information.\n\n## How to use\n\nYou can run a simple lambda function with a timeout.\n\n``` C++\ntimeout(1s, []() {\n  std::this_thread::sleep_for(0.8s);\n});\n```\n\nYou can run a function pointer with arguments and return value, with a timeout:\n\n``` C++\nint my_sum(int a, int b) {\n  std::this_thread::sleep_for(0.5s);\n  return a + b;\n}\n\n// main\nint j = timeout(1s, my_sum, 5, 6);\n```\n\nYou can run a functor with or without argument or return value:\n\n``` C++\nstruct Bar {\n  int c;\n\n  Bar(int c) : c(c) {}\n\n  int operator()(int b) {\n    return b + c + 3;\n  }\n};\n\n// main\nBar b(10);\nauto k = timeout(1s, b, 10);\n```\n\n\u003e Note: to use chrono literals like `0.8s` please do `using namespace std::literals::chrono_literals;`\n\u003e beforehand.\n\n## Install\n\nMaybe just copy the `src/timeout.hpp` file directly?\n\n## Test\n\n```\n$ make\n$ ./tests/test\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliby99%2Ftimeout","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliby99%2Ftimeout","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliby99%2Ftimeout/lists"}