-

   rss_forum_sources_ru

 - e-mail

 

 -

 LiveInternet.ru:
: 29.07.2007
:
:
: 80

:


TLS ("thread-local storage", "transport layer security")

, 13 2020 . 17:13 +
Qraizer: . static , , . static , thread_local . , .


:
    // ------------ thrd.h
    #ifdef THREAD_LOCAL
    extern thread_local int trdLocalVar;
    #else
    static int trdLocalVar = -1;
    #endif
    void g();
    // ------------ src1.cpp
    #include
    #include
    #include
    #include
    #include "thrd.h"
    std::mutex out;
    void g()
    {
    std::lock_guard ct(out);
    std::cout << std::hex << std::setw(8) << std::this_thread::get_id() << ": the trdLocalVar is "
    << std::dec << trdLocalVar << " now" << std::endl;
    }
    // ------------ src2.cpp
    #include
    #include
    #include
    #include
    #include "thrd.h"
    #ifdef THREAD_LOCAL
    thread_local int trdLocalVar = -1;
    #endif
    std::mutex mtx;
    std::array events;
    void thr(int n)
    {
    trdLocalVar = n;
    g();
    std::lock_guard ev(events[n-1]);
    std::lock_guard ct(mtx);
    g();
    }
    int main()
    {
    using namespace std::literals;
    for (auto &i : events) i.lock();
    std::thread trd1(thr, 1);
    std::thread trd2(thr, 2);
    std::thread trd3(thr, 3);
    g();
    std::this_thread::sleep_for(2s);
    events[1].unlock();
    trd2.join();
    g();
    events[0].unlock();
    trd1.join();
    g();
    events[2].unlock();
    trd3.join();
    g();
    }
THREAD_LOCAL:
    bc4: the trdLocalVar is 1 now
    f1c: the trdLocalVar is 2 now
    198c: the trdLocalVar is 3 now
    654: the trdLocalVar is -1 now
    f1c: the trdLocalVar is 2 now
    654: the trdLocalVar is -1 now
    bc4: the trdLocalVar is 1 now
    654: the trdLocalVar is -1 now
    198c: the trdLocalVar is 3 now
    654: the trdLocalVar is -1 now
: "... is -1 now".


P.S. thread_local extern . src1 src2 .

https://forum.sources.ru/index.php?showtopic=419143&view=findpost&p=3834198

:  

: [1] []
 

:
: 

: ( )

:

  URL