以下最小代码示例的较大程序将命令从客户端线程发送到asio io_service对象.io_service对象(在Ios类中)正在使用一个线程运行.发送命令时,客户端线程会等待,直到Ios对象(通过Cmd :: NotifyFinish())通知它已完成.
这个示例似乎在Linux Ubuntu 11.04上运行,提升1.46很好但在Windows 7上提升1.46它断言.
我怀疑它与Cmd :: NotifyFinish()中的锁有关.当我将锁移出嵌套范围时,以便在锁的范围内调用waitConditionVariable_.notify_one()时,它不会在Windows 7上崩溃.但是,boost :: thread文档指出notify_one()不需要在锁内被召唤.
堆栈跟踪(下面)显示它在调用notify_one()时处于断言状态.就好像cmd对象在调用notify之前消失了......
如何使这个线程安全而不断言?
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/bind.hpp>
#include <iostream>
class Cmd
{
public:
Cmd() : cnt_(0), waitPred_(false), waiting_(false)
{
}
virtual ~Cmd()
{
}
void BindInfo(int CmdSeq)
{
cnt_ = CmdSeq;
}
void NotifyFinish()
{
// call by service thread...
{
boost::mutex::scoped_lock lock(waitMutex_);
waitPred_ = true;
if (!waiting_)
{
// don't need to notify as isn't waiting …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
////
// Default Namespaces
///
using namespace std;
typedef map <string, boost::shared_mutex> t_map_shared_mutex;
int main(int argc, char** argv) {
t_map_shared_mutex list_lock;
boost::shared_mutex global_lock;
string i = "ABC";
boost::unique_lock < boost::shared_mutex > l_lock ( global_lock );
boost::unique_lock < boost::shared_mutex > lock ( list_lock[i] );
//Do Something with that lock
lock.unlock();
l_lock.unlock();
}
Run Code Online (Sandbox Code Playgroud)
这会生成以下错误.根据我的理解(我可能在这里非常错误)g ++告诉我,互斥量是作为const值传递的......我不明白为什么.
In file included from /usr/include/c++/4.4/utility:63,
from /usr/include/boost/config/no_tr1/utility.hpp:21,
from /usr/include/boost/config/select_stdlib_config.hpp:33,
from /usr/include/boost/config.hpp:40,
from /usr/include/boost/date_time/compiler_config.hpp:12,
from /usr/include/boost/date_time/posix_time/posix_time.hpp:14,
from prova.cpp:5:
/usr/include/c++/4.4/bits/stl_pair.h: In constructor ‘std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with …Run Code Online (Sandbox Code Playgroud) 我正在使用我从这个网站获得的阻塞队列示例,认为它非常好.此阻塞队列使用boost :: mutex.有时抛出异常:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
Run Code Online (Sandbox Code Playgroud)
what():错误的文件描述符
这是阻塞队列代码:
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/condition_variable.hpp>
#include <exception>
#include <list>
#include <stdio.h>
struct BlockingQueueTerminate
: std::exception
{};
namespace tools {
template<class T>
class BlockingQueue
{
private:
boost::mutex mtx_;
boost::condition_variable cnd_;
std::list<T> q_;
unsigned blocked_;
bool stop_;
public:
BlockingQueue()
: blocked_()
, stop_()
{}
~BlockingQueue()
{
this->stop(true);
}
void stop(bool wait)
{
// tell threads blocked on BlockingQueue::pull() to leave
boost::mutex::scoped_lock lock(mtx_);
stop_ = true;
cnd_.notify_all();
if(wait) …Run Code Online (Sandbox Code Playgroud) 将线程添加到boost :: thread_group时:
boost::thread_group my_threads;
boost::thread *t = new boost::thread( &someFunc );
my_threads.add_thread(th);
Run Code Online (Sandbox Code Playgroud)
只有当my_threads对象超出范围时,才会删除所有创建的boost :: thread对象.但是我的程序主线程在执行时产生了很多线程.因此,如果已经完成了大约50个线程,程序将使用大约1.5Gb的内存,并且仅在主进程终止时释放该内存.
问题是:如何在线程函数完成后删除这些boost :: thread对象?!
我是Boost线程的新手,我不知道如何从多个线程执行输出.我有一个简单的boost :: thread从9倒数到1; 主线程等待然后打印"LiftOff .. !!"
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
struct callable {
void operator() ();
};
void callable::operator() () {
int i = 10;
while(--i > 0) {
cout << "#" << i << ", ";
boost::this_thread::yield();
}
cout.flush();
}
int main() {
callable x;
boost::thread myThread(x);
myThread.join();
cout << "LiftOff..!!" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是我必须在我的线程中使用显式的"cout.flush()"语句来显示输出.如果我不使用flush(),我只会得到"LiftOff !!" 作为输出.
有人可以告诉我为什么需要明确使用flush()吗?
我正在为XCode 4.6中的iOS开发.我正在为服务编写一个库,并使用boost来启动线程.我的一个方法看起来像这样:
void Service::start(boost::shared_ptr<ResultListener> listener) {
boost::future<bool> con = boost::make_future(true);
listener->onConnected(); // Works
boost::future<bool> initDone = con.then([&, listener](boost::future<bool>& connected) {
listener->onConnected(); // Works
if (!connected.get()) {
listener->onError("...");
return false;
}
listener->onError("..."); // EXC_BAD_ACCESS
/* ... */
return true;
});
}
Run Code Online (Sandbox Code Playgroud)
在设备上执行此操作时,我会EXC_BAD_ACCESS在标记的行处获得.我对此感到非常惊讶,因为第一次调用onConnected是成功的,即使我在该onError调用之前添加了一个调用if也是有效的.
由于对C++缺乏经验,我会对每一条信息感到高兴,这些信息是关于什么原因,如何调试它以及如何在下次发生此问题时如何发现.此外,我不太确定哪些信息是相关的.我所知道的可能与我迄今为止所发现的相关,以下可能是:ResultListener并且Service是boost::noncopyable.我检查了shared_ptr(使用use_count)的引用计数,它在继续中增加.我正在使用boost 1.53.这个方法就是这样调用的
Servuce reco(/* ... */);
boost::shared_ptr<foo> f(new foo());
reco.start(f);
Run Code Online (Sandbox Code Playgroud)
foo是一个简单的类,std::cout如果调用一个方法,它只会打印.
编辑:进一步窥探让我检查get()调用,我发现以下代码future.hpp正在执行:
// …Run Code Online (Sandbox Code Playgroud) 堆栈溢出有几个例子,其中一个函数获得的升级锁第一和然后获得由升级的独占访问.我的理解是,如果不小心使用,这会导致死锁,因为两个线程都可以获得可升级/共享锁,然后两者都尝试升级,此时两者都不能继续,因为另一个具有共享锁.
我想要的是首先获得独占锁,然后降级到共享锁,而不完全释放锁.我找不到这样的例子.有任何想法吗?
我正在阅读文档部分boost::thread_specific_ptr,并尝试解析此段落:
注意:在某些平台上,不会对使用平台的本机API创建的线程执行特定于线程的数据的清理.在这些平台上,只有使用boost :: thread启动的线程才会进行此类清理,除非从该线程手动调用boost :: on_thread_exit().
首先,这可能是一个迂腐的观点:我认为他们的意思是说boost::this_thread::at_thread_exit()而不是boost::on_thread_exit().否则我真的迷路了.
更重要的是,线程究竟需要做什么?它是否足以将一些无操作函数传递给at_thread_exit(),还是需要传递其他东西?
(这个主题在这里的评论中讨论过,但我仍然不确定我需要做什么.)
(背景故事:我正在寻找解决我今天早些时候提出的问题的方法).
我需要建立与队列(生产者任务推到队列中,消费者执行它们,因为他们来)连接两个线程生产者 - 消费者方案.
由于队列在大多数情况下都是空的,所以我必须做到这一点,以便消费者线程可以睡觉,并在生产者推动某些东西后立即被唤醒.但是,我必须确保生产者永远不会被阻止,甚至不会很快.换句话说,我需要一些单侧阻塞队列.
有无锁队列,但由于这些是定义的,好吧,无锁,因此消费者线程不可能被它们阻止.
我曾想过将无锁队列与条件变量相关联.当消费者线程发现队列为空时,它将睡眠等待通知条件.生产者线程在将任务推入唤醒消费者线程的队列(如果它正在休眠)时会通知条件.但是,条件变量必须受互斥锁保护,这意味着在尝试获取生成器线程以通知条件时,生产者线程仍有很小的机会被阻止.
我还没有找到解决这个问题的好方法,所以你的想法更受欢迎.
注意:我打算使用boost线程来实现它.
注2:我不考虑生产者试图推送东西并且队列已满的情况.这永远不会发生.
哪个是c ++ boost线程库使用的线程模型?
1:1 (Kernel-level threading)
N:1 (User-level threading)
M:N (Hybrid threading)
这些模型之间的区别(来自wiki):http://en.wikipedia.org/wiki/Thread_ (computing)#Models
我检查了boost站点,并没有提到它使用的线程模型.
我猜这是一个1:1,因为它不提供像yield或的功能reschedule,但我不确定...
boost-thread ×10
c++ ×10
boost ×7
boost-asio ×1
boost-mutex ×1
exception ×1
ios6 ×1
mutex ×1
pthreads ×1