标签: boost-thread

信号和线程 - 设计决策的好坏?

我必须编写一个执行高度计算密集型计算的程序.该程序可能会运行几天.可以在不同的线程中轻松分离计算,而无需共享数据.我想要一个GUI或一个Web服务,告诉我当前的状态.

我目前的设计使用BOOST :: signals2和BOOST :: thread.它编译并到目前为止按预期工作.如果一个线程完成一次迭代并且有新数据可用,则它会调用一个连接到GUI类中的插槽的信号.

我的问题:

  • 这种信号和线程的组合是一个明智的想法吗?我是另一个论坛,有人建议别人不要"走这条路".
  • 附近是否有潜在的致命陷阱,我没有看到?
  • 我期望的现实,这将是"容易"用我的GUI类来提供Web界面或QT,一个VTK或任何窗口?
  • 有没有更聪明的选择(像其他升压库),我忽略了?

以下代码编译

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++ boost-thread boost-signals2

6
推荐指数
2
解决办法
5477
查看次数

如何立即取消卷曲操作?

我在C++中使用libcurl,并且我curl_easy_perform使用Boost.Thread在我的UI中调用一个单独的线程.

主UI有一个取消按钮,我想要完全响应(即,当用户点击它时,它应立即作出反应).我已经设置了读取,写入和进度回调来读取原子should_cancel变量(如问题所示),但有两个问题:

  1. 从按下取消到卷曲操作完成时,通常会有非常小的(但明显的)延迟.

  2. 偶尔会有很长的(有时是无休止的)延迟.在这种情况下,要么:

    一个.进度,读取和写入回调很长一段时间都没有被调用,或者

    湾 进度回调调用时,我返回一个非零值(这意味着它应该终止),但卷曲操作不一会儿完成更长(事实上,功能将被再次在此期间,被称为!)

所以:

  1. 为什么会发生长时间延迟(尤其是在没有调用进度函数的情况下)?
  2. 我应该怎么做才能让取消按钮正确反应?

一种可能性是告诉UI取消操作成功,但继续在后台运行curl线程,直到取消.这个问题(我认为)是它强制should_cancel变量是全局的,而不是作用于操作开始的对话框.

c++ multithreading curl boost-thread

6
推荐指数
2
解决办法
1万
查看次数

如何在boost tcp/udp服务器中处理control-c

如何处理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)

c++ boost-thread boost-asio

6
推荐指数
2
解决办法
4241
查看次数

如何使用boost使类成员函数成为线程函数

我打算写一个适配器类.在这个类中有一个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)

c++ linux boost xml-rpc boost-thread

6
推荐指数
2
解决办法
1万
查看次数

C++中的共享队列

我只是简单地从网络获取数据包,并将它们排入一个线程,然后在另一个线程中使用此数据包(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++ queue multithreading boost boost-thread

6
推荐指数
1
解决办法
1万
查看次数

多线程C++消息传递

我的任务是修改同步C程序,以便它可以并行运行.我们的目标是让它尽可能便携,因为它是许多人使用的开源程序.因此,我认为最好将程序包装在C++层中,这样我就可以利用便携式的boost库.我已经完成了这一切,一切似乎按预期工作.

我遇到的问题是决定在线程之间传递消息的最佳方法是什么.幸运的是,该程序的体系结构是多生产者和单个消费者的体系结构.更好的是,消息的顺序并不重要.我已经读过单一生产者/单一消费者(SPSC)队列将受益于这种架构.那些有多线程编程经验的人有什么建议吗?我对这些东西很新.此外,任何使用boost实现SPSC的代码示例都将非常感激.

c++ multithreading boost boost-thread

6
推荐指数
1
解决办法
8648
查看次数

如何在标准C++ 11中创建shared_lock或upgrade_lock?

我从新标准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.

c++ multithreading mutex boost-thread c++11

6
推荐指数
1
解决办法
3161
查看次数

如何使用"-pthread"而不是"-mthread"编译boost_thread?

我有一个操作系统,编译没有-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)

c++ boost compilation pthreads boost-thread

6
推荐指数
1
解决办法
4670
查看次数

线程安全的有界队列在Boost 1.54中挂起

以下用于有界线程安全队列的代码用于在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)

c++ multithreading boost boost-thread

6
推荐指数
1
解决办法
939
查看次数

Boost.Thread在1.58中太晚醒来

我有一个应用程序,需要在某些窗口内工作(在这种情况下,窗口相隔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)

c++ boost boost-thread

6
推荐指数
1
解决办法
906
查看次数