Apparently class type thread_local variable with are initialized
dynamically, not at load time. This means every time the
thread_local variable is accessed, the code checks to see if
the variable needs initialization. This doesn't appear to be
the case for native types.
This caused the c++ version of smrproxy to go from about
0.6 nanoseconds for a lock()/unlock() operation in c to
about 3.2 nanoseconds.
On 11/22/2024 4:42 AM, jseigh wrote:
Apparently class type thread_local variable with are initialized
dynamically, not at load time. This means every time the
thread_local variable is accessed, the code checks to see if
the variable needs initialization. This doesn't appear to be
the case for native types.
This caused the c++ version of smrproxy to go from about
0.6 nanoseconds for a lock()/unlock() operation in c to
about 3.2 nanoseconds.
Shit. Humm...
thread_local struct ct_per_thread* ct_g_per_thread = nullptr;
ct_per_thread*
proxy_register_thread()
{
if (! ct_g_per_thread)
{
thread_local ct_per_thread l_ct_per_thread(_whatever_);
ct_g_per_thread = &l_ct_per_thread;
}
return ct_g_per_thread;
}
void proxy_lock()
{
ct_per_thread* per_thread = ct_g_per_thread;
assert(per_thread);
}
void proxy_unlock()
{
ct_per_thread* per_thread = ct_g_per_thread;
assert(per_thread);
}
If those asserts trip then it means that proxy_register_thread was not
called before them. Humm... Change the API to accept a pointer to a ct_per_thread... ;^)
void proxy_lock(ct_per_thread* per_thread)
{
}
void proxy_unlock(ct_per_thread* per_thread)
{
}
void test()
{
ct_per_thread* per_thread = proxy_register_thread();
for (unsigned long i = 0; i < 1000000; ++i)
{
proxy_lock(per_thread);
//... do you thing!
proxy_unlock(per_thread);
}
}
The dtor of ct_per_thread would set ct_g_per_thread to 0?
For any registered thread, ct_g_per_thread is valid can can be accessed
in any function it calls.
Is that crap, or kind of crap?
It should work okay.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 493 |
Nodes: | 16 (2 / 14) |
Uptime: | 158:14:17 |
Calls: | 9,700 |
Files: | 13,732 |
Messages: | 6,179,650 |