的boost :: unique_lock :: timed_lock的使用 [英] Usage of boost::unique_lock::timed_lock

查看:1216
本文介绍了的boost :: unique_lock :: timed_lock的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提振:: timed_lock

void wait(int seconds) 
{ 
  boost::this_thread::sleep(boost::posix_time::seconds(seconds)); 
} 

boost::timed_mutex mutex; 

void thread() 
{ 
  for (int i = 0; i < 5; ++i) 
  { 
    wait(1); 
    boost::unique_lock<boost::timed_mutex> lock(mutex, boost::try_to_lock); 
    if (!lock.owns_lock()) 
      lock.timed_lock(boost::get_system_time() + boost::posix_time::seconds(1));//<<<<
    std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl; 
    boost::timed_mutex *m = lock.release(); 
    m->unlock(); 
  } 
}

<一个href=\"http://www.boost.org/doc/libs/1_41_0/doc/html/thread/synchronization.html#thread.synchronization.mutex_concepts.lockable.try_lock\"相对=nofollow> timed_lock

问题>我有问题要了解以下行:

Question> I have problems to understand the following lines:

  if (!lock.owns_lock()) 
     lock.timed_lock(boost::get_system_time() + 
                     boost::posix_time::seconds(1));//<<<<

下面是我的理解。假设 lock.owns_lock()返回false,这意味着当前对象不拥有可锁定对象的锁。因此,下一行会被执行。如果指定的时间已过和对象仍无法获得锁后,那么的boost :: timed_lock 将返回false。所以,下面一行将被执行???

Here is my understanding. Assume lock.owns_lock() returns false which means the current object DOES NOT own the lock on the lockable object. So next line will be executed. If after the specified time lapsed and the object still cannot get the lock, then the boost::timed_lock will return false. So the following line will be executed???

std::cout << "Thread " << boost::this_thread::get_id() << ": " << i << std::endl; 

这种想法正确吗?我认为,code的目的是确保在执行上面的线当且仅当对象具有锁。但根据我的理解(我猜是不正确的),上面的行总是被运行!

Is this idea correct? I think the purpose of the code is to make sure the above line is executed iff the object has the lock. But based on my understanding(i guess is NOT correct), the above line always gets run!

问题出在哪里?

推荐答案

您是对的,这个例子不保证锁始终正确执行保护code之前获得的。

You are right and the example does NOT guarantee the lock is always properly acquired before executing the protected code.

由于下面的例子说明:

以上程序传递的boost :: try_to_lock作为第二个参数的boost :: unique_lock的构造函数。是否互斥已经获取可以通过owns_lock()方法之后进行检查。如果它没有 - owns_lock()返回false - 通过的boost :: unique_lock提供的另外一个函数:timed_lock()等待一段时间以获得互斥体。给定的程序等待长达1秒的应该是足够的时间来获取互斥体了。

The above program passes boost::try_to_lock as the second parameter to the constructor of boost::unique_lock. Whether or not the mutex has been acquired can be checked via the owns_lock() method afterwards. In case it has not - owns_lock() returns false - another function provided by boost::unique_lock is used: timed_lock() waits for a certain time to acquire the mutex. The given program waits for up to one second which should be more than enough time to acquire the mutex.

这个例子实际上显示获取互斥的三个基本途径:锁()等待,直到互斥体已经被收购。 try_lock()不会等待,但获取该互斥锁,如果它是在呼叫的时间,否则返回false。最后,timed_lock()尝试给定的时间期限内获得互斥体。正如try_lock(),成功或失败被bool类型的返回值表示。

The example actually shows the three fundamental ways of acquiring a mutex: lock() waits until the mutex has been acquired. try_lock() does not wait but acquires the mutex if it is available at the time of the call and returns false otherwise. Finally, timed_lock() tries to acquire the mutex within a given period of time. As with try_lock(), success or failure is indicated by the return value of type bool.

的作者似乎意识到了这个问题(假设该文件的返回值 timed_lock ),但没想到重新测试,如果锁已经被收购,需要(按他们说的证明等待长达1秒的应该是足够的时间来获取更多的互斥体)。

the authors seem aware of the problem (given that the document the return value of timed_lock) but did not think a re-test if the lock had been acquired was needed (as demonstrated by them saying "waits for up to one second which should be more than enough time to acquire the mutex").

在您的理解的一个错误:

One error in your understanding:

如果在指定的时间已过,后对象仍然无法获得锁,然后升压:: timed_lock将返回false。

If after the specified time lapsed and the object still cannot get the lock, then the boost::timed_lock will return false.

这是不正确的。 timed_lock 将不断尝试获取锁,但是放弃,如果在指定的时间已过期。

This is not true. timed_lock will 'continuously' try to obtain the lock, but give up if the specified time has expired.

这篇关于的boost :: unique_lock :: timed_lock的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆