示例#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 execute_usb_command(uvc::device & device, std::timed_mutex & mutex, unsigned char handle_id, uint8_t *out, size_t outSize, uint32_t & op, uint8_t * in, size_t & inSize)
    {
        // write
        errno = 0;

        int outXfer;

        if (!mutex.try_lock_for(std::chrono::milliseconds(IVCAM_MONITOR_MUTEX_TIMEOUT))) throw std::runtime_error("timed_mutex::try_lock_for(...) timed out");
        std::lock_guard<std::timed_mutex> guard(mutex, std::adopt_lock);

        bulk_transfer(device, handle_id, IVCAM_MONITOR_ENDPOINT_OUT, out, (int)outSize, &outXfer, 1000); // timeout in ms

        std::this_thread::sleep_for(std::chrono::milliseconds(20));
        // read
        if (in && inSize)
        {
            uint8_t buf[IVCAM_MONITOR_MAX_BUFFER_SIZE];

            errno = 0;

            bulk_transfer(device, handle_id, IVCAM_MONITOR_ENDPOINT_IN, buf, sizeof(buf), &outXfer, 1000);
            if (outXfer < (int)sizeof(uint32_t)) throw std::runtime_error("incomplete bulk usb transfer");

            op = *(uint32_t *)buf;
            if (outXfer > (int)inSize) throw std::runtime_error("bulk transfer failed - user buffer too small");
            inSize = outXfer;
            memcpy(in, buf, inSize);
        }
    }
示例#3
1
        void bulk_usb_command(uvc::device & device, std::timed_mutex & mutex, unsigned char out_ep, uint8_t *out, size_t outSize, uint32_t & op, unsigned char in_ep, uint8_t * in, size_t & inSize, int timeout)
        {
            // write
            errno = 0;

            int outXfer;

            if (!mutex.try_lock_for(std::chrono::milliseconds(timeout))) throw std::runtime_error("timed_mutex::try_lock_for(...) timed out");
            std::lock_guard<std::timed_mutex> guard(mutex, std::adopt_lock);

            bulk_transfer(device, out_ep, out, (int)outSize, &outXfer, timeout); // timeout in ms

            // read
            if (in && inSize)
            {
                uint8_t buf[1024];  // TBD the size may vary

                errno = 0;

                bulk_transfer(device, in_ep, buf, sizeof(buf), &outXfer, timeout);
                if (outXfer < (int)sizeof(uint32_t)) throw std::runtime_error("incomplete bulk usb transfer");

                op = *(uint32_t *)buf;
                if (outXfer > (int)inSize) throw std::runtime_error("bulk transfer failed - user buffer too small");
                inSize = outXfer;
                memcpy(in, buf, inSize);
            }
        }
示例#4
1
		void lock()
		{
			using namespace std::chrono;

			if (_m.try_lock())
				return;

			detail::threading_primitive_guard g;

			if (g.should_detect_deadlocks())
			{
				auto d = milliseconds(TimeoutMs_);
				detail::timer t;
				while (!_m.try_lock_for(d))
					LoggerPolicy_::show_message(detail::make_log_message("Could not lock mutex", get_id(), t.elapsed()));
			}
			else
				_m.lock();
		}
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);
        }
    }
}
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
}
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();
}
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;
}
/**
 * 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;
}
void f2()
{
    time_point t0 = Clock::now();
    assert(m.try_lock_until(Clock::now() + ms(250)) == false);
    time_point t1 = Clock::now();
    ns d = t1 - t0 - ms(250);
    assert(d < ms(50));  // within 50ms
}
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();
    }
}
示例#12
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;
}
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;
}
示例#14
0
void fireworks() {
	// 这边未什么用while呢?因为没获得锁的时候不能往下走调用unlock会引发abort
	if (!mtimetx.try_lock_for(std::chrono::milliseconds(200))) {
		std::cout << "草,没有获得锁" << endl;
	}
	std::cout << "获得锁!!!!!!!" << endl;
	std::this_thread::sleep_for(std::chrono::milliseconds(1000));
	std::cout << "*\n";
	//mtimetx.unlock();

}
示例#15
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);
        }
    }
}
示例#16
0
		void unlock()
		{ _m.unlock(); }
示例#17
0
		bool try_lock()
		{ return _m.try_lock(); }
示例#18
0
namespace node_ios_device {

const double notificationWait = 0.5;

CFMutableDictionaryRef connectedDevices;
std::mutex deviceMutex;
CFRunLoopRef runloop;
std::timed_mutex initMutex;
static bool initialized = false;
static am_device_notification deviceNotification = NULL;
static CFRunLoopTimerRef initTimer;

static void stopInitTimer() {
	if (initTimer) {
		debug("Removing init timer from run loop");
		::CFRunLoopTimerInvalidate(initTimer);
		::CFRunLoopRemoveTimer(runloop, initTimer, kCFRunLoopCommonModes);
		::CFRelease(initTimer);
		initTimer = NULL;
	}
}

static void unlockInitMutex(CFRunLoopTimerRef timer, void* info) {
	initialized = true;
	initMutex.unlock();
	stopInitTimer();
}

static void createInitTimer() {
	// set a timer for 250ms to unlock the initMutex
	CFRunLoopTimerContext timerContext = { 0, NULL, NULL, NULL, NULL };
	initTimer = ::CFRunLoopTimerCreate(
		kCFAllocatorDefault, // allocator
		CFAbsoluteTimeGetCurrent() + notificationWait, // fireDate
		0, // interval
		0, // flags
		0, // order
		unlockInitMutex,
		&timerContext
	);

	debug("Adding init timer to run loop");
	::CFRunLoopAddTimer(runloop, initTimer, kCFRunLoopCommonModes);
}

/**
 * The callback when a device notification is received.
 */
static void on_device_notification(am_device_notification_callback_info* info, void* arg) {
	bool changed = false;

	debug("Resetting timer due to new device notification");
	stopInitTimer();

	// ensure that the device is connected via USB
	if (::AMDeviceGetInterfaceType(info->dev) == 1) {
		if (info->msg == ADNCI_MSG_CONNECTED) {
			std::lock_guard<std::mutex> lock(deviceMutex);
			node_ios_device::Device* device = new node_ios_device::Device(info->dev);

			if (!::CFDictionaryContainsKey(connectedDevices, device->udid)) {
				node_ios_device::debug("Device connected");

				try {
					device->init();

					// if we already have the device info, don't get it again
					if (device->loaded && !::CFDictionaryContainsKey(connectedDevices, device->udid)) {
						::CFDictionarySetValue(connectedDevices, device->udid, device);
						changed = true;
					}
				} catch (...) {
					node_ios_device::debug("Failed to init device");
					delete device;
				}
			}

		} else if (info->msg == ADNCI_MSG_DISCONNECTED) {
			std::lock_guard<std::mutex> lock(deviceMutex);
			CFStringRef udid = ::AMDeviceCopyDeviceIdentifier(info->dev);

			if (::CFDictionaryContainsKey(connectedDevices, udid)) {
				// remove the device from the dictionary and destroy it
				node_ios_device::Device* device = (node_ios_device::Device*)::CFDictionaryGetValue(connectedDevices, udid);
				::CFDictionaryRemoveValue(connectedDevices, udid);

				node_ios_device::debug("Device disconnected: %s", device->props["udid"].c_str());

				delete device;
				changed = true;
			}
		}
	}

	if (!initialized) {
		createInitTimer();
	}

	// we need to notify if devices changed and this must be done outside the
	// scopes above so that the mutex is unlocked
	if (changed) {
		node_ios_device::send("devicesChanged");
	}
}

/**
 * Starts the run loop.
 */
void startRunLoop() {
	connectedDevices = ::CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, NULL);

	node_ios_device::debug("Subscribing to device notifications");
	::AMDeviceNotificationSubscribe(&on_device_notification, 0, 0, NULL, &deviceNotification);

	runloop = ::CFRunLoopGetCurrent();

	createInitTimer();

	node_ios_device::debug("Starting CoreFoundation run loop");
	::CFRunLoopRun();
}

/**
 * Stops the run loop.
 */
void stopRunLoop() {
	// free up connected devices
	CFIndex size = ::CFDictionaryGetCount(connectedDevices);
	CFStringRef* keys = (CFStringRef*)::malloc(size * sizeof(CFStringRef));
	::CFDictionaryGetKeysAndValues(connectedDevices, (const void **)keys, NULL);
	CFIndex i = 0;

	for (; i < size; i++) {
		Device* device = (Device*)::CFDictionaryGetValue(connectedDevices, keys[i]);
		::CFDictionaryRemoveValue(connectedDevices, keys[i]);
		delete device;
	}

	::free(keys);
	::CFRelease(connectedDevices);
	::AMDeviceNotificationUnsubscribe(deviceNotification);
	stopInitTimer();
	::CFRunLoopStop(runloop);
}

} // end namespace node_ios_device
示例#19
0
static void unlockInitMutex(CFRunLoopTimerRef timer, void* info) {
	initialized = true;
	initMutex.unlock();
	stopInitTimer();
}