移动构造函数的Boost.Thread库规范声明为
移动构造函数:
效果:将由其他(如果有)管理的线程的所有权转移到新构造的boost :: thread实例.
而对于移动分配:
将其他(如果有)管理的线程的所有权转移给*this.如果有一个先前与*this相关联的线程,那么该线程将被分离.
显然,对于移动分配,明确声明先前的线程对象变得分离.
由于尚未定义boost :: thread对象的复制,并且只能移动所有权,这是否意味着使用Move Constructor,以前的所有者线程不会分离?
或者这只是一个文件疏忽?
我在编译使用线程的程序时遇到错误.这是导致问题的部分.如果有人告诉我是否以正确的方式调用线程函数,那将会很好.
在main.cpp中:
int main()
{
WishList w;
boost::thread thrd(&w.show_list);
thrd.join();
}
Run Code Online (Sandbox Code Playgroud)
在another_file.cpp中:
class WishList{
public:
void show_list();
}
void WishList::show_list(){
.
.
.
.
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say ‘&WishList::show_list’
/home/sharatds/Downloads/boost_1_46_1/boost/thread/detail/thread.hpp: In member function ‘void boost::detail::thread_data<F>::run() [with F = void (WishList::*)()]’:
/home/sharatds/Downloads/boost_1_46_1/boost/thread/detail/thread.hpp:61:17: error: must use ‘.*’ or ‘->*’ to call pointer-to-member function in ‘((boost::detail::thread_data<void (WishList::*)()>*)this)->boost::detail::thread_data<void (WishList::*)()>::f (...)’, e.g. ‘(... ->* ((boost::detail::thread_data<void (WishList::*)()>*)this)->boost::detail::thread_data<void (WishList::*)()>::f) …Run Code Online (Sandbox Code Playgroud) 我正在玩boost库和C++.我想创建一个包含生产者,conumer和堆栈的多线程程序.procuder填充堆栈,消费者从堆栈中删除items(int).一切正常(pop,push,mutex)但是当我把pop/push winthin称为线程时,我没有任何效果
我做了这个简单的代码:
#include "stdafx.h"
#include <stack>
#include <iostream>
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
#include <boost/signals2/mutex.hpp>
#include <ctime>
using namespace std;
/ *
* this class reprents a stack which is proteced by mutex
* Pop and push are executed by one thread each time.
*/
class ProtectedStack{
private :
stack<int> m_Stack;
boost::signals2::mutex m;
public :
ProtectedStack(){
}
ProtectedStack(const ProtectedStack & p){
}
void push(int x){
m.lock();
m_Stack.push(x);
m.unlock();
}
void pop(){
m.lock();
//return m_Stack.top();
if(!m_Stack.empty())
m_Stack.pop(); …Run Code Online (Sandbox Code Playgroud) 我使用的是boost_1_49_0版本.我想更改项目中使用的boost命名空间.我尝试了以下方式 -
试图通过以下方式在名为XYZ的项目中添加自定义命名空间
namespace XYZ
{
.... Corresponding boost Headers
}
Run Code Online (Sandbox Code Playgroud)
但未能这样做.有一些编译错误.
我想使用boost命名空间如下
XYZ::boost::thread
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
我正在尝试使用boost :: wait_condition来休眠一个线程,直到有一些新数据可用.我的功能减少到这个:
bool Node::waitForNewData() const
{
boost::unique_lock<boost::mutex>(mWasNotifiedMutex);
mWasNotified = false;
while (true)
{
if (mWasNotified)
return true;
if (mThreadIsRequestedToStop)
return false;
mWasNotifiedWaitCondition.wait(mWasNotifiedMutex);
}
}
Run Code Online (Sandbox Code Playgroud)
Boost使用以下消息从wait()函数抛出异常:
boost unique_lock has no mutex: Operation not permitted
Run Code Online (Sandbox Code Playgroud)
我正在使用这样的函数来通知等待条件:
void Node::callbackNewDataArrived()
{
{
boost::unique_lock<boost::mutex>(mHasNewInletDataMutex);
mWasNotified = true;
}
mWasNotifiedWaitCondition.notify_all();
}
Run Code Online (Sandbox Code Playgroud)
以及标题中的这些声明:
class Node
{
// ...
mutable bool mWasNotified;
mutable boost::mutex mWasNotifiedMutex;
mutable boost::condition_variable mWasNotifiedWaitCondition;
std::atomic<bool> mThreadIsRequestedToStop;
};
Run Code Online (Sandbox Code Playgroud)
我在Xcode 4.6.2中构建,在OSX 10.8.5上启用了c ++ 11支持.我的boost库是用它构建的
./b2 toolset=clang cxxflags="-std=c++11 -stdlib=libc++ -arch i386 -arch x86_64" macosx-version=10.6 linkflags="-stdlib=libc++" --prefix=/usr/local …Run Code Online (Sandbox Code Playgroud) 我有一个DirectX游戏,在双核系统上产生2个增强线程:1个用于游戏/渲染(通常在四核CPU上分成自己的线程),以及另外1个程序生成游戏世界的线程.我相信我的音频中间件也会产生自己的线程来播放SFX和音乐.
游戏总是在CPU上运行100%,这反过来可能会导致音频系统的一些溅射.我希望通过更好地管理Generation Thread的活动来减少CPU负载.虽然有时我需要它全速运行,但有时候(当玩家没有移动太多时)它只是不断更新而不是真的做了很多.
是否可以/建议手动管理线程的活动程度?如果是这样,我可以用什么策略来做到这一点?我一直看到人们说睡眠()功能并不是真的推荐,但我真的不知道还有什么可做的.
或者,我是通过尝试从线程管理中挤出循环来咆哮错误的树,我会通过传统的分析/优化更好地服务吗?
我正在运行以下代码块。此代码将创建 5 个从属线程和 1 个主线程。所有从线程等待主线程准备好数据,当数据准备好时,所有从线程将通知开始处理。
我的问题是,有可能在从线程开始等待之前conditional_variable,主线程准备好数据并通知等待的线程。在这种情况下,一些等待的线程将获得通知并开始处理,但未等待的线程将开始等待永远不会到来的通知。
如果您运行此示例,则不会发生这种情况,但我正在寻找一种方法来确保所有从属线程都在等待通知,然后通知它们。你知道我该怎么做吗?
/*
Condition Variables - Many waiting threads
Shows how one condition variable can be used to notify multiple threads
that a condition has occured.
* Part of "Threading with Boost - Part IV: Condition Variables", published at:
http://antonym.org/boost
Copyright (c) 2015 Gavin Baker <gavinb@antonym.org>
Published under the MIT license, see LICENSE for details
*/
#include <cstdio>
#include <boost/thread.hpp>
boost::condition_variable data_ready_cond;
boost::mutex data_ready_mutex;
bool data_ready = false;
void master_thread()
{
printf("+++ master …Run Code Online (Sandbox Code Playgroud) 我想让程序等待,直到它完成所有正在运行的线程ioService.stop();,这与一样,ioService无需等待即可停止。我尝试了以下代码,该代码工作正常,但ioService无需等待线程完成即可停止。
#include <iostream>
#include <boost/asio/io_service.hpp>
#include <boost/bind.hpp>
#include <boost/thread/thread.hpp>
void myTask (std::string &str);
int main(int argc, char **argv){
uint16_t total_threads = 4;
/*
* Create an asio::io_service and a thread_group
*/
boost::asio::io_service ioService;
boost::thread_group threadpool;
/*
* This will start the ioService processing loop.
*/
boost::asio::io_service::work work(ioService);
/*
* This will add threads to the thread pool.
*/
for (std::size_t i = 0; i < total_threads; ++i)
threadpool.create_thread(
boost::bind(&boost::asio::io_service::run, &ioService));
/*
* This will …Run Code Online (Sandbox Code Playgroud)