Demonstrates a usage and an effect of the chsv_thread_local macro.
#include <thread>
#include <mutex>
#include <iostream>
#include <vector>
int main(int, char**)
{
const unsigned int threads = 8;
std::mutex io_lock;
std::cout << "Initial values:\n\tfile_scope = " << file_scope << "\n\tblock_scope = " << block_scope << std::endl;
++file_scope; ++block_scope;
auto thr = [&]()->void
{
std::lock_guard<std::mutex> lock(io_lock);
++file_scope; ++block_scope;
std::cout << "A thread " << std::this_thread::get_id() <<
" with values captured by value:\n\tfile_scope = " << file_scope << "\n\tblock_scope = " << block_scope << std::endl;
};
std::vector<std::thread> vec;
for (unsigned i = 0; i < threads; ++i)
vec.emplace_back(thr);
for (auto& it:vec)
it.join();
std::wcout << "All threads terminated." << std::endl;
std::cout << "Final values:\n\tfile_scope = " << file_scope << "\n\tblock_scope = " << block_scope << std::endl;
return 0;
}
#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 ...