diff --git a/include/zmq.h b/include/zmq.h index 45c08e6a..873e913e 100644 --- a/include/zmq.h +++ b/include/zmq.h @@ -613,6 +613,7 @@ ZMQ_EXPORT void zmq_threadclose (void* thread); #define ZMQ_MSG_T_SIZE 6 #define ZMQ_THREAD_AFFINITY 7 #define ZMQ_THREAD_AFFINITY_DFLT -1 +#define ZMQ_THREAD_NAME_PREFIX 8 /* DRAFT Socket methods. */ ZMQ_EXPORT int zmq_join (void *s, const char *group); diff --git a/src/ctx.cpp b/src/ctx.cpp index f0145d39..294f9244 100644 --- a/src/ctx.cpp +++ b/src/ctx.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include "ctx.hpp" @@ -256,6 +257,13 @@ int zmq::ctx_t::set (int option_, int optval_) thread_affinity = optval_; } else + if (option_ == ZMQ_THREAD_NAME_PREFIX && optval_ >= 0) { + std::ostringstream s; + s << optval_; + scoped_lock_t locker(opt_sync); + thread_name_prefix = s.str(); + } + else if (option_ == ZMQ_BLOCKY && optval_ >= 0) { scoped_lock_t locker(opt_sync); blocky = (optval_ != 0); @@ -401,11 +409,16 @@ zmq::object_t *zmq::ctx_t::get_reaper () void zmq::ctx_t::start_thread (thread_t &thread_, thread_fn *tfn_, void *arg_) const { + static unsigned int nthreads_started = 0; + thread_.setSchedulingParameters(thread_priority, thread_sched_policy, thread_affinity); thread_.start(tfn_, arg_); #ifndef ZMQ_HAVE_ANDROID - thread_.setThreadName ("ZMQ background"); + std::ostringstream s; + s << thread_name_prefix << "/ZMQbg/" << nthreads_started; + thread_.setThreadName (s.str().c_str()); #endif + nthreads_started++; } void zmq::ctx_t::send_command (uint32_t tid_, const command_t &command_) diff --git a/src/ctx.hpp b/src/ctx.hpp index 85d41571..8f4d2e6a 100644 --- a/src/ctx.hpp +++ b/src/ctx.hpp @@ -211,10 +211,11 @@ namespace zmq // Is IPv6 enabled on this context? bool ipv6; - // Thread scheduling parameters. + // Thread parameters. int thread_priority; int thread_sched_policy; int thread_affinity; + std::string thread_name_prefix; // Synchronisation of access to context options. mutex_t opt_sync; diff --git a/src/zmq_draft.h b/src/zmq_draft.h index f568e3ae..7d947ae0 100644 --- a/src/zmq_draft.h +++ b/src/zmq_draft.h @@ -93,6 +93,7 @@ #define ZMQ_MSG_T_SIZE 6 #define ZMQ_THREAD_AFFINITY 7 #define ZMQ_THREAD_AFFINITY_DFLT -1 +#define ZMQ_THREAD_NAME_PREFIX 8 /* DRAFT Socket methods. */ int zmq_join (void *s, const char *group); diff --git a/tests/test_ctx_options.cpp b/tests/test_ctx_options.cpp index 18c668e7..9a0b19d1 100644 --- a/tests/test_ctx_options.cpp +++ b/tests/test_ctx_options.cpp @@ -124,6 +124,14 @@ void test_ctx_thread_opts(void* ctx) rc = zmq_ctx_set(ctx, ZMQ_THREAD_AFFINITY, cpu_affinity_test); assert (rc == 0); #endif + + +#ifdef ZMQ_THREAD_NAME_PREFIX + // test thread name prefix: + + rc = zmq_ctx_set(ctx, ZMQ_THREAD_NAME_PREFIX, 1234); + assert (rc == 0); +#endif }