Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #120746
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: thread_local question... |
| Date | 2024-10-20 19:12 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <vf4db2$l6cb$6@dont-email.me> (permalink) |
| References | <ve7cj8$2vun0$1@dont-email.me> <ve8f6l$34vt2$1@dont-email.me> <ve98fe$392tv$2@dont-email.me> <9dc17426ed663b192a4e1467b8fc488e6be377bd.camel@gmail.com> |
On 10/10/2024 12:20 PM, wij wrote:
[...]
Check this shit out:
Here is my crude code:
_______________________________________
#include <iostream>
#include <functional>
#include <thread>
#include <atomic>
#include <mutex>
#define CT_THREADS 5
struct ct_shared
{
std::mutex m_cout_lock;
};
struct ct_per_thread
{
ct_shared& m_shared;
unsigned long m_id;
ct_per_thread(
ct_shared& shared,
unsigned long id
): m_shared(shared),
m_id(id)
{
{
std::unique_lock<std::mutex> lock(m_shared.m_cout_lock);
std::cout << "ct_per_thread::ct_per_thread(" << m_id << ")"
<< std::endl;
}
}
~ct_per_thread()
{
{
std::unique_lock<std::mutex> lock(m_shared.m_cout_lock);
std::cout << "ct_per_thread::~ct_per_thread(" << m_id <<
")" << std::endl;
}
}
};
thread_local ct_per_thread* g_per_thread = nullptr;
void
ct_foo()
{
// Okay, what about this shit!
{
std::unique_lock<std::mutex>
lock(g_per_thread->m_shared.m_cout_lock);
std::cout << "ct_foo(" << g_per_thread->m_id << ")" << std::endl;
}
}
void
ct_thread(
ct_shared& shared,
unsigned long id
) {
{
thread_local ct_per_thread self(shared, id);
g_per_thread = &self;
}
ct_foo();
}
int
main()
{
std::cout << "ct_plot_pre_alpha... Testing 123! :^)\n\n";
std::cout << "_____________________________________________" <<
std::endl;
{
ct_shared shared;
{
std::thread threads[CT_THREADS];
std::cout << "launching " << CT_THREADS << " threads..." <<
std::endl;
for (unsigned long i = 0; i < CT_THREADS; ++i)
{
threads[i] = std::thread(ct_thread, std::ref(shared), i);
}
for (unsigned long i = 0; i < CT_THREADS; ++i)
{
threads[i].join();
}
}
}
std::cout << "_____________________________________________\n";
std::cout << "complete!\n";
return 0;
}
_______________________________________
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-09 19:02 -0700
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-09 19:08 -0700
Re: thread_local question... Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-10-09 19:33 -0700
Re: thread_local question... Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-10-09 19:40 -0700
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-09 20:08 -0700
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-09 20:19 -0700
Re: thread_local question... jseigh <jseigh_es00@xemaps.com> - 2024-10-10 07:52 -0400
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-10 12:04 -0700
Re: thread_local question... wij <wyniijj5@gmail.com> - 2024-10-11 03:20 +0800
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-20 19:12 -0700
Re: thread_local question... wij <wyniijj5@gmail.com> - 2024-10-21 19:06 +0800
Re: thread_local question... wij <wyniijj5@gmail.com> - 2024-10-21 19:20 +0800
Re: thread_local question... jseigh <jseigh_es00@xemaps.com> - 2024-10-10 15:57 -0400
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-11 13:36 -0700
Re: thread_local question... jseigh <jseigh_es00@xemaps.com> - 2024-10-11 09:50 -0400
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-11 12:21 -0700
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-11 22:53 -0700
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-11 22:56 -0700
Re: thread_local question... jseigh <jseigh_es00@xemaps.com> - 2024-10-11 17:05 -0400
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-12 13:44 -0700
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-12 13:46 -0700
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-12 13:29 -0700
Re: thread_local question... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-10-12 13:43 -0700
csiph-web