From 112ef6f1721ab1059729e8a77ee3c9f32f0b77d4 Mon Sep 17 00:00:00 2001 From: Joel Lauener Date: Wed, 25 Jun 2014 09:51:10 +0200 Subject: [PATCH] Allow change of pthread priority Rationale: In a real-time environment it is sometime mandatory to tune threads priority and scheduling policy. This is required by our users who mixes real-time and server threads within the same process. It's not planned to support this on non-pthread platforms (e.g. Windows). --- src/thread.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/thread.cpp b/src/thread.cpp index 52a0195a..2dcd2616 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -62,6 +62,7 @@ void zmq::thread_t::stop () #else #include +#include extern "C" { @@ -83,12 +84,36 @@ extern "C" } } +bool getenvi(const char *env_, int &result_) +{ + char *str = getenv(env_); + if(str == NULL) + { + return false; + } + + std::stringstream ss(str); + return ss >> result_; +} + void zmq::thread_t::start (thread_fn *tfn_, void *arg_) { tfn = tfn_; arg = arg_; int rc = pthread_create (&descriptor, NULL, thread_routine, this); posix_assert (rc); + + int prio; + if(getenvi("ZMQ_THREAD_PRIO", prio)) + { + int policy = SCHED_RR; + getenvi("ZMQ_THREAD_POLICY", policy); + + struct sched_param param; + param.sched_priority = prio; + rc = pthread_setschedparam(descriptor, policy, ¶m); + posix_assert (rc); + } } void zmq::thread_t::stop ()