我对锁和互斥锁之间的区别感到非常困惑.在Boost文档中,它说,
锁定类型
互斥体类型
在另一篇文章中,我看到这样的函数,
boost::shared_mutex _access;
void reader()
{
boost::shared_lock< boost::shared_mutex > lock(_access);
// do work here, without anyone having exclusive access
}
void conditional_writer()
{
boost::upgrade_lock< boost::shared_mutex > lock(_access);
// do work here, without anyone having exclusive access
if (something) {
boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);
// do work here, but now you have exclusive access
}
// do more work here, without anyone having …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <string>
#include <boost/thread/tss.hpp>
static boost::thread_specific_ptr<string> _tssThreadNameSptr;
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
g ++ -c -I $ BOOST_PATH tssNaming.h
tssNaming.h:7:错误:未在此范围内声明'string'
但我在我的包括字符串#include.
我正在编写一个阻止两个输入的应用程序istreams.
从两者中读取istream是同步(阻塞)调用,因此,我决定创建两个Boost::threads来进行读取.
这些线程中的任何一个都可以到达"结束"(基于接收的一些输入),并且一旦达到"结束",两个输入流都停止接收.不幸的是,我不知道哪个会这样做.
因此,我不能join()在两个线程上,因为只有一个线程(不能预定哪一个)将实际返回(解除阻塞).
我必须以某种方式强迫对方退出,但它被阻止等待输入,所以它本身不能决定是时候返回(条件变量或什么不是).
他们是一种方式:
istream"失败",或注意:
istreams就是cin编辑:
谢谢!
我有一个关于使用boost::lock_guard(或类似的作用域锁)和使用应该由return语句中的锁保护的变量的问题.
如何破坏本地对象并复制返回值?返回值优化如何影响这一点?
例:
Data Class::GetData()
{
boost::lock_guard<boost::mutex> lock(this->mMutex);
return this->mData;
}
Run Code Online (Sandbox Code Playgroud)
这是正确的(如果mData是受mMutex保护的变量)吗?或者我是否必须使用本地范围和临时,如下例所示:
Data Class::GetData()
{
Data ret;
{
boost::lock_guard<boost::mutex> lock(this->mMutex);
ret = this->mData;
}
return ret;
}
Run Code Online (Sandbox Code Playgroud) 存在一种众所周知的锁定多个锁的方法,其依赖于根据该排序选择固定的线性排序和获取锁.
例如,在"获取锁定两个互斥锁并避免死锁"的答案中提出了这一点.特别是,基于地址比较的解决方案似乎非常优雅和明显.
当我试图检查它是如何实际实现的时候,令我惊讶的是,我发现这个解决方案没有被广泛使用.
教科书会告诉你,如果你总是以相同的顺序锁定,你将永远不会遇到这种僵局.实践会告诉你这种方法不能扩展:当我创建一个新的锁时,我不太了解内核,无法确定它适合的5000锁定层次结构中的位置.
PThreads似乎根本没有内置这样的机制.
Boost.Thread提出了完全不同的解决方案,lock()因为多个(2到5个)互斥锁基于尝试并锁定尽可能多的互斥锁.
这是Boost.Thread源代码的片段(Boost 1.48.0,boost/thread/locks.hpp:1291):
template<typename MutexType1,typename MutexType2,typename MutexType3>
void lock(MutexType1& m1,MutexType2& m2,MutexType3& m3)
{
unsigned const lock_count=3;
unsigned lock_first=0;
for(;;)
{
switch(lock_first)
{
case 0:
lock_first=detail::lock_helper(m1,m2,m3);
if(!lock_first)
return;
break;
case 1:
lock_first=detail::lock_helper(m2,m3,m1);
if(!lock_first)
return;
lock_first=(lock_first+1)%lock_count;
break;
case 2:
lock_first=detail::lock_helper(m3,m1,m2);
if(!lock_first)
return;
lock_first=(lock_first+2)%lock_count;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
其中成功lock_helper返回0和未成功锁定的互斥锁数量.
为什么这个解决方案比地址或任何其他类型的ID 更好?我没有看到指针比较有任何问题,使用这种"盲"锁定可以避免这种问题.
关于如何在库级别解决此问题还有其他想法吗?
thread_ = boost::thread( boost::function< void (void)>( boost::bind( &clientTCP::run , this ) ) );
Run Code Online (Sandbox Code Playgroud)
是否有可能run有这样的参数:
void clientTCP::run(boost:function<void(std::string)> func);
Run Code Online (Sandbox Code Playgroud)
如果是,我应该如何编写我的boost :: thread调用
谢谢.
我试图了解不同的用例.和2线程使用之间的区别.
这是我读过的一个很棒的教程boost::thread_group.
这是我正在使用的代码:
boost::threadpool::pool s_ThreadPool(GetCoreCount());
CFilterTask task(pFilter, // filter to run
boost::bind(&CFilterManagerThread::OnCompleteTask, this, _1, _2) // OnComplete sync callback // _1 will be filter name // _2 will be error code
);
// schedule the new task - runs on the threadpool
s_ThreadPool.schedule(task);
Run Code Online (Sandbox Code Playgroud)
这是析构函数:
s_ThreadPool.wait(0);
Run Code Online (Sandbox Code Playgroud)
你能解释一下吗?
是否可以为a命名,boost::thread以便调试器表和崩溃日志更具可读性?怎么样?
我boost::thread使用new运算符创建对象并继续而不等待此线程完成其工作:
void do_work()
{
// perform some i/o work
}
boost::thread *thread = new boost::thread(&do_work);
Run Code Online (Sandbox Code Playgroud)
我想,有必要删除thread工作完成时.没有明确等待线程终止,最好的方法是什么?
我有阻塞任务,将由find_the_question()函数执行.但是,我不希望线程执行此函数需要超过10秒.因此,如果它需要超过10秒,我想关闭该线程清理所有资源.
我尝试为此编写代码,但不知何故,如果线程占用时间超过10秒,我无法在find_the_question()函数中获得中断.你能告诉我我做错了什么吗?
void find_the_question(std::string value)
{
//allocate x resources
try{
//do some process on resources
sleep(14);
//clean resources
}
catch(boost::thread_interrupted const& )
{
//clean resources
std::cout << "Worker thread interrupted" << std::endl;
}
}
int main()
{
boost::posix_time::time_duration timeout = boost::posix_time::milliseconds(10000);
std::cout << "In main" << std::endl;
boost::thread t1(find_the_question, "Can you block me");
t1.interrupt();
if (t1.timed_join(timeout))
{
//finished
std::cout << "Worker thread finished" << std::endl;
}
else
{
//Not finished;
std::cout << "Worker thread not finished" << std::endl;
}
std::cout …Run Code Online (Sandbox Code Playgroud) boost-thread ×10
c++ ×10
boost ×3
mutex ×2
boost-asio ×1
debugging ×1
interrupt ×1
iostream ×1
locking ×1