我必须编写一个执行高度计算密集型计算的程序.该程序可能会运行几天.可以在不同的线程中轻松分离计算,而无需共享数据.我想要一个GUI或一个Web服务,告诉我当前的状态.
我目前的设计使用BOOST :: signals2和BOOST :: thread.它编译并到目前为止按预期工作.如果一个线程完成一次迭代并且有新数据可用,则它会调用一个连接到GUI类中的插槽的信号.
我的问题:
以下代码编译
g++ -Wall -o main -lboost_thread-mt <filename>.cpp
Run Code Online (Sandbox Code Playgroud)
代码如下:
#include <boost/signals2.hpp>
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <iterator>
#include <string>
using std::cout;
using std::cerr;
using std::string;
/**
* Called when a CalcThread finished a new bunch of data.
*/
boost::signals2::signal<void(string)> signal_new_data;
/**
* The whole data will be stored here.
*/
class DataCollector
{
typedef boost::mutex::scoped_lock scoped_lock;
boost::mutex mutex;
public:
/**
* Called by CalcThreads call the …Run Code Online (Sandbox Code Playgroud) 我在C++中使用libcurl,并且我curl_easy_perform使用Boost.Thread在我的UI中调用一个单独的线程.
主UI有一个取消按钮,我想要完全响应(即,当用户点击它时,它应立即作出反应).我已经设置了读取,写入和进度回调来读取原子should_cancel变量(如本问题所示),但有两个问题:
从按下取消到卷曲操作完成时,通常会有非常小的(但明显的)延迟.
偶尔会有很长的(有时是无休止的)延迟.在这种情况下,要么:
一个.进度,读取和写入回调很长一段时间都没有被调用,或者
湾 进度回调被调用时,我返回一个非零值(这意味着它应该终止),但卷曲操作不一会儿完成更长(事实上,功能将被再次在此期间,被称为!)
所以:
一种可能性是告诉UI取消操作成功,但继续在后台运行curl线程,直到取消.这个问题(我认为)是它强制should_cancel变量是全局的,而不是作用于操作开始的对话框.
如何处理control-C事件或停止我的boost :: asio服务器.我有一个tcp&udp组合服务器,并希望能够在按ctrl-c时干净利落地退出.我得到了未处理控件-C的第一次机会异常.这是我的代码
void startTCP()
{
http::syncServer::server serv( 2);
// Set console control handler to allow server to be stopped.
// console_ctrl_function = boost::bind(&http::syncServer::server::stop, &serv);
//SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
// Run the server until stopped.
serv.run();
}
void startUDP()
{
boost::asio::io_service io_service;
http::syncServer::udp_server server(io_service);
// console_ctrl_function = boost::bind(&http::syncServer::udp_server::stop, &server);
// SetConsoleCtrlHandler(console_ctrl_handler, TRUE);
io_service.run();
}
int main(int argc, char* argv[])
{
try
{
boost::shared_ptr<boost::thread> tcpThread( new boost::thread(startTCP));
boost::shared_ptr<boost::thread> udpThread (new boost::thread(startUDP));
/*console_ctrl_function = boost::bind(&http::syncServer::udp_server::stop, &server);
SetConsoleCtrlHandler(console_ctrl_handler, FALSE);*/
tcpThread->join();
udpThread->join();
}
catch (std::exception& e) …Run Code Online (Sandbox Code Playgroud) 我打算写一个适配器类.在这个类中有一个xmlrpc-c服务器(深渊服务器).我想通过创建一个新线程来启动服务器,并且线程的功能是成员函数XMLThreadFun().
当我尝试使用下面的代码时,在适配器的构造函数实现的行上有一个错误:
/usr/include/boost/bind/bind.hpp:69:37: error: ‘void (Adapter::*)()’ is not a class, struct, or union type
Run Code Online (Sandbox Code Playgroud)
谁能告诉我如何解决这个错误,或者如何实现我的目标?对此,我真的非常感激.
以下是我的代码片段:
#ifdef _MSC_VER
#pragma warning( disable : 4503 4355 4786 )
#else
#include "config.h"
#endif
#include "quickfix/FileStore.h"
#include "quickfix/SocketInitiator.h"
#include "quickfix/SessionSettings.h"
#include "Application.h"
#include <string>
#include <iostream>
#include <fstream>
#include "quickfix/SessionID.h"
#include "quickfix/Session.h"
#include "getopt-repl.h"
#include <cassert>
#include <xmlrpc-c/base.hpp>
#include <xmlrpc-c/registry.hpp>
#include <xmlrpc-c/server_abyss.hpp>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
using namespace std;
class theClient : public xmlrpc_c::method {
public:
theClient() {}
theClient(FIX::SocketInitiator* initiator) {
set<FIX::SessionID> …Run Code Online (Sandbox Code Playgroud) 我只是简单地从网络获取数据包,并将它们排入一个线程,然后在另一个线程中使用此数据包(Dequeue).
所以我决定使用boost库来建立一个基于https://www.quantnet.com/cplusplus-multithreading-boost/的共享队列
template <typename T>
class SynchronisedQueue
{
private:
std::queue<T> m_queue; // Use STL queue to store data
boost::mutex m_mutex; // The mutex to synchronise on
boost::condition_variable m_cond;// The condition to wait for
public:
// Add data to the queue and notify others
void Enqueue(const T& data)
{
// Acquire lock on the queue
boost::unique_lock<boost::mutex> lock(m_mutex);
// Add the data to the queue
m_queue.push(data);
// Notify others that data is ready
m_cond.notify_one();
} // Lock is automatically released …Run Code Online (Sandbox Code Playgroud) 我的任务是修改同步C程序,以便它可以并行运行.我们的目标是让它尽可能便携,因为它是许多人使用的开源程序.因此,我认为最好将程序包装在C++层中,这样我就可以利用便携式的boost库.我已经完成了这一切,一切似乎按预期工作.
我遇到的问题是决定在线程之间传递消息的最佳方法是什么.幸运的是,该程序的体系结构是多生产者和单个消费者的体系结构.更好的是,消息的顺序并不重要.我已经读过单一生产者/单一消费者(SPSC)队列将受益于这种架构.那些有多线程编程经验的人有什么建议吗?我对这些东西很新.此外,任何使用boost实现SPSC的代码示例都将非常感激.
我从新标准std::shared_lock模板类中非常缺少.在Boost.Thread中boost::shared_lock,甚至boost::upgrade_lock存在.
为什么这样,std::unique_lock在C++ 11中没有std :: shared_lock ?
如何获得类似的行为boost::shared_lock,但在纯C++ 11中呢?
我正在考虑使用boost::shared_lock<std::mutex>,但这没有那么多意义,因为std::mutex没有lock_shared()成员.而且,没有这样的std::shared_mutex.
我有一个操作系统,编译没有-mthread可用.我有-pthread.如何用而不是编译boost_thread?-pthread-mthread
我当前的编译器构建日志:
./b2 -j1 --with-thread link=static --prefix=./install-dir release threading=multi --builddir=./bu
ild-dir install
Component configuration:
- chrono : not building
- context : not building
- date_time : not building
- exception : not building
- filesystem : not building
- graph : not building
- graph_parallel : not building
- iostreams : not building
- locale : not building
- math : not building
- mpi : not building
- program_options : …Run Code Online (Sandbox Code Playgroud) 以下用于有界线程安全队列的代码用于在Boost 1.49中按预期工作.但是,在更新到Boost 1.54之后,代码不再按预期运行.也就是说,当缓冲区为空(完整)时,消费者线程(生产者线程)将永远等待m_not_empty(m_not_full)条件变量并且永远不会唤醒(我认为因为生产者线程没有互斥锁).
版本1.54中是否有一些可能会破坏代码的更改?或者,也许,我错过了代码中的错误?
#include <iostream>
#include <boost/circular_buffer.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/thread.hpp>
template <class T>
class bounded_buffer {
public:
bounded_buffer(size_t capacity) {cb.set_capacity(capacity);}
void push(T item) {
boost::mutex::scoped_lock lock(m_mutex);
while (cb.full()) {
m_not_full.wait(lock);
}
cb.push_back(item);
lock.unlock();
m_not_empty.notify_one();
}
void pop(T &pItem) {
boost::mutex::scoped_lock lock(m_mutex);
while (cb.empty()) {
m_not_empty.wait(lock);
}
pItem = cb.front();
cb.pop_front();
lock.unlock();
m_not_full.notify_one();
}
private:
boost::mutex m_mutex;
boost::condition m_not_empty;
boost::condition m_not_full;
boost::circular_buffer<T> cb;
};
bounded_buffer<int> bb_int(4);
void producer() {
int i = 10;
for(int j=0; j<100; ++j) …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,需要在某些窗口内工作(在这种情况下,窗口相隔30秒).当时间不在窗口内时,计算下一个窗口中间的时间,并且线程休眠该时间量(以毫秒为单位,使用boost::this_thread::sleep_for).
使用Boost 1.55,我能够在极限可靠性的范围内(+/- 100ms)击中窗户.在迁移到Boost 1.58后,我无法打到这些窗口.替换boost::this_thread::sleep_forwith std::this_thread::sleep_for修复了问题; 但是,我需要提供的可中断功能boost::thread和中断点boost::this_thread::sleep_for.
以下是一些说明问题的示例代码:
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <chrono>
#include <iostream>
#include <thread>
void boostThreadFunction ()
{
std::cout << "Starting Boost thread" << std::endl;
for (int i = 0; i < 10; ++i)
{
auto sleep_time = boost::chrono::milliseconds {29000 + 100 * i};
auto mark = std::chrono::steady_clock::now ();
boost::this_thread::sleep_for (sleep_time);
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now () - mark);
std::cout << "Boost thread:" << std::endl;
std::cout << …Run Code Online (Sandbox Code Playgroud) boost-thread ×10
c++ ×10
boost ×6
boost-asio ×1
c++11 ×1
compilation ×1
curl ×1
linux ×1
mutex ×1
pthreads ×1
queue ×1
xml-rpc ×1