Add new util function

This commit is contained in:
topjohnwu
2019-05-25 21:42:51 -07:00
parent 6785dc4967
commit 23d3e56967
4 changed files with 16 additions and 8 deletions

View File

@@ -208,6 +208,7 @@ int parse_int(S __s) { return parse_int(__s.data()); }
int new_daemon_thread(void *(*start_routine) (void *), void *arg = nullptr,
const pthread_attr_t *attr = nullptr);
int new_daemon_thread(std::function<void()> &&fn);
struct exec_t {
bool err = false;

View File

@@ -200,6 +200,17 @@ int new_daemon_thread(void *(*start_routine) (void *), void *arg, const pthread_
return ret;
}
static void *proxy_routine(void *fp) {
auto fn = reinterpret_cast<std::function<void()>*>(fp);
(*fn)();
delete fn;
return nullptr;
}
int new_daemon_thread(std::function<void()> &&fn) {
return new_daemon_thread(proxy_routine, new std::function<void()>(std::move(fn)));
}
static char *argv0;
static size_t name_len;
void init_argv0(int argc, char **argv) {