C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Suggestion: non-static member variables for static-duration-only classes

From: Thiago Macieira <thiago_at_[hidden]>
Date: Fri, 03 Oct 2025 21:53:56 -0700
On Friday, 3 October 2025 20:45:52 Pacific Daylight Time Walt Karas wrote:
> thread_local Per_thread pt{*this};
> std::mutex mtx;
> std::set<Per_thread *> pt_active;
> };
>
>
> Counter tcp_packets, udp_packets;

You can't have per-instance per-thread variables. This doesn't exist. Choose
one or the other.

The solution to this is to have a static thread_local std::vector of the per-
instance content and the instance has an ID (the index of the vector) for the
entry.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center Group

Received on 2025-10-04 04:54:05