Exemplo n.º 1
0
int main()
{
#if _LIBCPP_STD_VER > 11
    {
        m.lock();
        std::vector<std::thread> v;
        for (int i = 0; i < 5; ++i)
            v.push_back(std::thread(f1));
        std::this_thread::sleep_for(ms(250));
        m.unlock();
        for (auto& t : v)
            t.join();
    }
    {
        m.lock();
        std::vector<std::thread> v;
        for (int i = 0; i < 5; ++i)
            v.push_back(std::thread(f2));
        std::this_thread::sleep_for(ms(300));
        m.unlock();
        for (auto& t : v)
            t.join();
    }
#endif  // _LIBCPP_STD_VER > 11
}
int main(int, char**)
{
    {
        m.lock();
        std::vector<std::thread> v;
        for (int i = 0; i < 5; ++i)
            v.push_back(std::thread(f1));
        std::this_thread::sleep_for(WaitTime);
        m.unlock();
        for (auto& t : v)
            t.join();
    }
    {
        m.lock();
        std::vector<std::thread> v;
        for (int i = 0; i < 5; ++i)
            v.push_back(std::thread(f2));
        std::this_thread::sleep_for(WaitTime + Tolerance);
        m.unlock();
        for (auto& t : v)
            t.join();
    }

  return 0;
}
Exemplo n.º 3
0
  void flushingWork()
  {
#if 0
    std::vector< LoggerPtr > loggersList;
    loggersList.reserve(loggers.size());
    mutex.lock_shared();
    for (auto it = loggers.begin(); it != loggers.end(); ++it)
    {
      loggersList.push_back(it->second);
    }
    mutex.unlock_shared();


    for (auto it = loggersList.begin(); it != loggersList.end(); ++it)
    {
      (*it)->flush();
    }
    std::this_thread::yield();
#endif
#if 1
    mutex.lock_shared();
    for (auto it = loggers.begin(); it != loggers.end(); ++it)
    {
      auto logger = it->second;
      logger->flush(); // flushing inside a CS may be not a good idea...
    }
    mutex.unlock_shared();
    std::this_thread::yield();
#endif
}
Exemplo n.º 4
0
void g()
{
    time_point t0 = Clock::now();
    m.lock_shared();
    time_point t1 = Clock::now();
    m.unlock_shared();
    ns d = t1 - t0;
    assert(d < Tolerance);  // within tolerance
}
Exemplo n.º 5
0
void f()
{
    time_point t0 = Clock::now();
    m.lock();
    time_point t1 = Clock::now();
    m.unlock();
    ns d = t1 - t0 - ms(250);
    assert(d < ms(50));  // within 50ms
}
Exemplo n.º 6
0
void f1()
{
    time_point t0 = Clock::now();
    assert(m.try_lock_for(WaitTime + Tolerance) == true);
    time_point t1 = Clock::now();
    m.unlock();
    ns d = t1 - t0 - WaitTime;
    assert(d < Tolerance);  // within tolerance
}
Exemplo n.º 7
0
void f1()
{
    time_point t0 = Clock::now();
    assert(m.try_lock_for(ms(300)) == true);
    time_point t1 = Clock::now();
    m.unlock();
    ns d = t1 - t0 - ms(250);
    assert(d < ms(50));  // within 50ms
}
void f1()
{
    time_point t0 = Clock::now();
    assert(m.try_lock_shared_until(Clock::now() + WaitTime + Tolerance) == true);
    time_point t1 = Clock::now();
    m.unlock_shared();
    ns d = t1 - t0 - WaitTime;
    assert(d < Tolerance);  // within 50ms
}
Exemplo n.º 9
0
int main()
{
#if _LIBCPP_STD_VER > 11
    m.lock();
    std::thread t(f);
    std::this_thread::sleep_for(ms(250));
    m.unlock();
    t.join();
#endif  // _LIBCPP_STD_VER > 11
}
int main()
{
    m.lock();
    std::vector<std::thread> v;
    for (int i = 0; i < 5; ++i)
        v.push_back(std::thread(f));
    std::this_thread::sleep_for(ms(250));
    m.unlock();
    for (auto& t : v)
        t.join();
}
Exemplo n.º 11
0
void f()
{
    time_point t0 = Clock::now();
    assert(!m.try_lock());
    assert(!m.try_lock());
    assert(!m.try_lock());
    while(!m.try_lock())
        ;
    time_point t1 = Clock::now();
    m.unlock();
    ns d = t1 - t0 - ms(250);
    assert(d < ms(200));  // within 200ms
}
void f()
{
    time_point t0 = Clock::now();
    assert(!m.try_lock_shared());
    assert(!m.try_lock_shared());
    assert(!m.try_lock_shared());
    while(!m.try_lock_shared())
        ;
    time_point t1 = Clock::now();
    m.unlock_shared();
    ns d = t1 - t0 - ms(250);
    assert(d < Tolerance);  // within tolerance
}
void f2()
{
    time_point t0 = Clock::now();
    assert(m.try_lock_shared_until(Clock::now() + WaitTime) == false);
    time_point t1 = Clock::now();
    ns d = t1 - t0 - WaitTime;
    assert(d < Tolerance);  // within tolerance
}
Exemplo n.º 14
0
int main()
{
    {
        m.lock();
        std::thread t(f1);
        std::this_thread::sleep_for(WaitTime);
        m.unlock();
        t.join();
    }
    {
        m.lock();
        std::thread t(f2);
        std::this_thread::sleep_for(WaitTime + Tolerance);
        m.unlock();
        t.join();
    }
}
Exemplo n.º 15
0
void f2()
{
    time_point t0 = Clock::now();
    assert(m.try_lock_for(ms(250)) == false);
    time_point t1 = Clock::now();
    ns d = t1 - t0 - ms(250);
    assert(d < ms(50));  // within 50ms
}
Exemplo n.º 16
0
int main()
{
    m.lock();
    std::vector<std::thread> v;
    for (int i = 0; i < 5; ++i)
        v.push_back(std::thread(f));
    std::this_thread::sleep_for(WaitTime);
    m.unlock();
    for (auto& t : v)
        t.join();
    m.lock_shared();
    for (auto& t : v)
        t = std::thread(g);
    std::thread q(f);
    std::this_thread::sleep_for(WaitTime);
    m.unlock_shared();
    for (auto& t : v)
        t.join();
    q.join();
}
Exemplo n.º 17
0
int main()
{
#if _LIBCPP_STD_VER > 11
    m.lock();
    std::vector<std::thread> v;
    for (int i = 0; i < 5; ++i)
        v.push_back(std::thread(f));
    std::this_thread::sleep_for(ms(250));
    m.unlock();
    for (auto& t : v)
        t.join();
    m.lock_shared();
    for (auto& t : v)
        t = std::thread(g);
    std::thread q(f);
    std::this_thread::sleep_for(ms(250));
    m.unlock_shared();
    for (auto& t : v)
        t.join();
    q.join();
#endif  // _LIBCPP_STD_VER > 11
}