chsvlib
chsv helper source code
chsvthrd\chsv_thread_local_get.cpp

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 "chsvthrd.h"
#include <vector>
#include <string>
#include <mutex>
#include <thread>
#include <sstream>
chsv_thread_local(std::vector<std::wstring>, vec);
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 = [&]()
{
chsv_thread_local_get(vec).push_back(L"Thread");
std::wostringstream os;
os << std::this_thread::get_id();
chsv_thread_local_get(vec).push_back(os.str());
chsv_thread_local_get(vec).push_back(L"is");
chsv_thread_local_get(vec).push_back(L"here!");
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;
}
/*
Output:
=======
Thread 666276 is here!
Thread 667868 is here!
Thread 664312 is here!
Thread 668648 is here!
Thread 667388 is here!
Thread 667776 is here!
Thread 666024 is here!
Thread 667128 is here!
*/
#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 ...