I've got a c++ linux project composed of separate producer and consumer executables. Producer has multiple threads, and each thread has a different shared memory area to be used for interprocess communication. Threads are started by a third-party library and I can't easily pass custom parameters to them.
My problem is what I need a way to setup threads to write to different areas of shared memory to avoid races and locks.
So far I came up with the following solution:
static size_t get_thread_index() {
static std::mutex once;
std::lock_guard<std::mutex> guard(once);
static std::map<pid_t, size_t> id_map;
pid_t tid = syscall(SYS_gettid);
auto it = id_map.find(tid);
if (it != id_map.end()) {
return it->second;
} else {
assert(id_map.size() < SHM_QUEUE_MAX_NODES);
return (id_map[tid] = id_map.size());
}
}
and then inside the producer
static thread_local size_t node_id = get_thread_index();
(*shm_q)[write_node_id] ...
But this solution feels like total overkill for such a simple task.
I can't just use stuff like syscall(SYS_gettid) % NUMBER_OF_THREADS because there's no guarantee thread id's would be sequential.
Are there an easy way of doing this?