void fn() {
try {
while(1) {
this_thread::interruption_point();//
Sleep(1);
}
} catch(...) {}
}
thread thrd(fn);
thrd.join();
// to interrupt the thrd, call this in some other thread
//thrd.interrupt();
how does this_thread::interruption_point() work?
i looked into the source, it calls the get_current_thread_data() to get some data that belongs to the thread:
now i know on win32, boost uses TLS to store the thread data, but still don't understand how the function get_current_thread_data works. It depends on current_thread_tls_key, but it seems to be a global value,
every time a thread starts, the current_thread_tls_key changes.
Thread classes use a data structure (the class instance) to hold per-thread information. Strickly speaking, the use of TLS is a nicety. If thread data isn't stored in the OS's thread local storage facility, it can be held in the class, it's no big deal.
Boost is using the class to hold the TLS index. So it's using a mixture of both techniques. It's holding current_thread_tls_key in the class instance, and that just points into the OS's TLS mechanism.
As you can see, it's not necessary, and will fail if you run out of TLS slots.