Пример #1
1
void thread_function_increase_timemutex()
{
    for (int i=0; i<5; i++)
    {
        if(g_counter_time_mutex.try_lock_for(std::chrono::seconds(1)))
            //g_counter_mutex.lock();
        {
            ++g_counter;
            cout << this_thread::get_id() << ": " << i << endl;
            g_counter_time_mutex.unlock();
            this_thread::sleep_for(std::chrono::seconds(2));
        }
    }
}
Пример #2
1
void test(const std::string& threadName){
    std::chrono::milliseconds timeout(100);

    for(int i = 0; i < 10; ++i){

		// Give up trying to acquire the lock after a period of time.
        if(timeLock.try_lock_for(timeout)){
            std::cout << threadName << " - locked mutex." << std::endl;

			std::chrono::milliseconds sleepDuration(500);
			std::this_thread::sleep_for(sleepDuration);

            timeLock.unlock();
        } else {
            std::cout << threadName << " - failed to lock mutex." << std::endl;

            std::chrono::milliseconds sleepDuration(250);
            std::this_thread::sleep_for(sleepDuration);
        }
    }
}
int main()
{
    {
        m.lock();
        std::thread t(f1);
        std::this_thread::sleep_for(ms(250));
        m.unlock();
        t.join();
    }
    {
        m.lock();
        std::thread t(f2);
        std::this_thread::sleep_for(ms(300));
        m.unlock();
        t.join();
    }
}
void f1()
{
    time_point t0 = Clock::now();
    assert(m.try_lock_until(Clock::now() + ms(300)) == true);
    time_point t1 = Clock::now();
    m.unlock();
    ns d = t1 - t0 - ms(250);
    assert(d < ms(50));  // within 50ms
}
Пример #5
0
int main(int, char**)
{
    {
        m.lock();
        std::thread t(f1);
        std::this_thread::sleep_for(ms(250));
        m.unlock();
        t.join();
    }
    {
        m.lock();
        std::thread t(f2);
        std::this_thread::sleep_for(ms(300));
        m.unlock();
        t.join();
    }

  return 0;
}
void fireworks () {
  // waiting to get a lock: each thread prints "-" every 200ms:
  while (!mtx.try_lock_for(std::chrono::milliseconds(200))) {
    std::cout << "-";
  }
  // got a lock! - wait for 1s, then this thread prints "*"
  std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  std::cout << "*\n";
  mtx.unlock();
}
Пример #7
0
int tc_libcxx_thread_thread_timedmutex_class_try_lock_until(void)
{
    {
        m.lock();
        std::thread t(f1);
        std::this_thread::sleep_for(ms(250));
        m.unlock();
        t.join();
    }
    {
        m.lock();
        std::thread t(f2);
        std::this_thread::sleep_for(ms(300));
        m.unlock();
        t.join();
    }
    TC_SUCCESS_RESULT();
    return 0;
}
Пример #8
0
static int f1()
{
    time_point t0 = Clock::now();
    TC_ASSERT_EXPR(m.try_lock_until(Clock::now() + ms(300)) == true);
    time_point t1 = Clock::now();
    m.unlock();
    ns d = t1 - t0 - ms(250);
    TC_ASSERT_EXPR(d < ms(50));  // within 50ms
    return 0;
}
Пример #9
0
/**
 * main thread that locks the mutex, starts thread then unlocks the mutex after
 * 2 seconds. That allows child thread to lock the mutex.
 * */
int main()
{
	mutex.lock();
	LOG(" M mutex locked");

	std::thread t(f);
	sleep(2);
	mutex.unlock();
	LOG(" M mutex unlocked");
	sleep(2);
	t.join();
	return 0;
}
Пример #10
0
void work(){
    std::chrono::milliseconds timeout(100);

    while(true){
        if(mutex.try_lock_for(timeout)){
            std::cout << std::this_thread::get_id() << ": do work with the mutex" << std::endl;

            std::chrono::milliseconds sleepDuration(250);
            std::this_thread::sleep_for(sleepDuration);

            mutex.unlock();

            std::this_thread::sleep_for(sleepDuration);
        } else {
            std::cout << std::this_thread::get_id() << ": do work without mutex" << std::endl;

            std::chrono::milliseconds sleepDuration(100);
            std::this_thread::sleep_for(sleepDuration);
        }
    }
}
Пример #11
0
static void unlockInitMutex(CFRunLoopTimerRef timer, void* info) {
	initialized = true;
	initMutex.unlock();
	stopInitTimer();
}
Пример #12
0
		void unlock()
		{ _m.unlock(); }