From 5ae860588394ad1512782c22f8104ceecebc1753 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 20 Aug 2013 00:07:01 +0900 Subject: [PATCH] src: Use std::thread --- src/shrpx_listen_handler.cc | 16 ++++++++-------- src/shrpx_worker.cc | 4 +--- src/shrpx_worker.h | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/shrpx_listen_handler.cc b/src/shrpx_listen_handler.cc index e889478f..7f6d1eae 100644 --- a/src/shrpx_listen_handler.cc +++ b/src/shrpx_listen_handler.cc @@ -25,9 +25,10 @@ #include "shrpx_listen_handler.h" #include -#include #include +#include +#include #include @@ -60,10 +61,6 @@ void ListenHandler::create_worker_thread(size_t num) num_worker_ = 0; for(size_t i = 0; i < num; ++i) { int rv; - pthread_t thread; - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); WorkerInfo *info = &workers_[num_worker_]; rv = socketpair(AF_UNIX, SOCK_STREAM, 0, info->sv); if(rv == -1) { @@ -72,9 +69,12 @@ void ListenHandler::create_worker_thread(size_t num) } info->sv_ssl_ctx = sv_ssl_ctx_; info->cl_ssl_ctx = cl_ssl_ctx_; - rv = pthread_create(&thread, &attr, start_threaded_worker, info); - if(rv != 0) { - LLOG(ERROR, this) << "pthread_create() failed: errno=" << rv; + try { + auto thread = std::thread{start_threaded_worker, info}; + thread.detach(); + } catch(const std::system_error& error) { + LLOG(ERROR, this) << "Could not start thread: code=" << error.code() + << " msg=" << error.what(); for(size_t j = 0; j < 2; ++j) { close(info->sv[j]); } diff --git a/src/shrpx_worker.cc b/src/shrpx_worker.cc index 5328120b..5286434f 100644 --- a/src/shrpx_worker.cc +++ b/src/shrpx_worker.cc @@ -90,12 +90,10 @@ void Worker::run() delete receiver; } -void* start_threaded_worker(void *arg) +void start_threaded_worker(WorkerInfo *info) { - WorkerInfo *info = reinterpret_cast(arg); Worker worker(info); worker.run(); - return 0; } } // namespace shrpx diff --git a/src/shrpx_worker.h b/src/shrpx_worker.h index 6e0a3aec..f7d9ffe6 100644 --- a/src/shrpx_worker.h +++ b/src/shrpx_worker.h @@ -46,7 +46,7 @@ private: SSL_CTX *cl_ssl_ctx_; }; -void* start_threaded_worker(void *arg); +void start_threaded_worker(WorkerInfo *info); } // namespace shrpx