src: Use std::thread

This commit is contained in:
Tatsuhiro Tsujikawa 2013-08-20 00:07:01 +09:00
parent da384988cd
commit 5ae8605883
3 changed files with 10 additions and 12 deletions

View File

@ -25,9 +25,10 @@
#include "shrpx_listen_handler.h"
#include <unistd.h>
#include <pthread.h>
#include <cerrno>
#include <thread>
#include <system_error>
#include <event2/bufferevent_ssl.h>
@ -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]);
}

View File

@ -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<WorkerInfo*>(arg);
Worker worker(info);
worker.run();
return 0;
}
} // namespace shrpx

View File

@ -46,7 +46,7 @@ private:
SSL_CTX *cl_ssl_ctx_;
};
void* start_threaded_worker(void *arg);
void start_threaded_worker(WorkerInfo *info);
} // namespace shrpx