Demonstrates a usage and an effect of the chsv_thread_local macro with compound objects as well as a usage of the chsv_thread_local_get macro.
#include <iostream>
#include <vector>
#include <string>
#include <mutex>
#include <thread>
#include <sstream>
std::wostream& operator<<(std::wostream& os, std::vector<std::wstring>& vec)
{
for (auto&s:vec)
std::wcout << s << " ";
return os;
}
int main(int, char**)
{
const int nThreads = 8;
std::vector<std::thread> threads;
std::mutex mtx;
auto thr = [&]()
{
std::wostringstream os;
os << std::this_thread::get_id();
std::lock_guard<std::mutex> lock(mtx);
std::wcout << vec << std::endl;
};
for (auto i = 0; i < nThreads; ++i)
threads.emplace_back(std::thread(thr));
for (auto& i:threads)
i.join();
return 0;
}
#define chsv_thread_local_get(name)
Resolves a name of a thread-local variable.
Definition: chsvthrd.h:1571
#define chsv_thread_local(type, name)
A macro simplifying work with thread-specific storages. It is introduced to implement syntax similar ...
Definition: chsvthrd.h:1565
An interface for the portable implementation of threads as defined in C11 standard supplemented with ...