Пример #1
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
}
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 < ms(200));  // within 200ms
}
Пример #3
0
    /**
     * lookup method: just traverse the list in order, and see if we find the
     * val
     */
    bool lookup(int val)
    {
        mtx.lock_shared();
        Node* curr = sentinel->next;

        while (curr != NULL) {
            if (curr->val >= val)
                break;
            curr = curr->next;
        }
	bool tmp =  ((curr != NULL) && (curr->val == val));
	mtx.unlock_shared();
        return tmp;
    }
Пример #4
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();
}