当前位置: 首页>>代码示例>>C++>>正文


C++ timed_mutex::try_lock_for方法代码示例

本文整理汇总了C++中std::timed_mutex::try_lock_for方法的典型用法代码示例。如果您正苦于以下问题:C++ timed_mutex::try_lock_for方法的具体用法?C++ timed_mutex::try_lock_for怎么用?C++ timed_mutex::try_lock_for使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在std::timed_mutex的用法示例。


在下文中一共展示了timed_mutex::try_lock_for方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: execute_usb_command

    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);
        }
    }
开发者ID:ddiakopoulos,项目名称:librealsense,代码行数:29,代码来源:hw-monitor.cpp

示例2: bulk_usb_command

        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);
            }
        }
开发者ID:dorodnic,项目名称:librealsense,代码行数:28,代码来源:ds-private.cpp

示例3: fireworks

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();
}
开发者ID:DanBrennan33,项目名称:SenecaOOP345-attic,代码行数:10,代码来源:mutex-try_lock_for.cpp

示例4: fireworks

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();

}
开发者ID:linxiubao,项目名称:C-11Concurrency,代码行数:11,代码来源:Demo2.cpp

示例5: thread_function_increase_timemutex

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));
        }
    }
}
开发者ID:guyueguakenan,项目名称:StudyProjects,代码行数:14,代码来源:Mutex.cpp

示例6: lock

		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();
		}
开发者ID:koplyarov,项目名称:stalemate,代码行数:19,代码来源:mutex.hpp

示例7: work

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);
        }
    }
}
开发者ID:yin8086,项目名称:cpp11,代码行数:21,代码来源:timedmutex.cpp

示例8: test

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);
        }
    }
}
开发者ID:DForshner,项目名称:CPPExperiments,代码行数:21,代码来源:RecursiveMutex.cpp


注:本文中的std::timed_mutex::try_lock_for方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。