相关疑难解决方法(0)

使用boost asio的线程池

我正在尝试使用boost :: asio创建一个有限的线程池类.但是我被困在某一点可以帮助我.

唯一的问题是我应该减少反击的地方?

代码无法按预期工作.

问题是我不知道我的线程何时完成执行以及我将如何知道它已经返回池中

#include <boost/asio.hpp>
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/bind.hpp>
#include <boost/thread/mutex.hpp>
#include <stack>

using namespace std;
using namespace boost;

class ThreadPool
{
    static int count;
    int NoOfThread;
    thread_group grp;
    mutex mutex_;
    asio::io_service io_service;
    int counter;
    stack<thread*> thStk ;

public:
    ThreadPool(int num)
    {   
        NoOfThread = num;
        counter = 0;
        mutex::scoped_lock lock(mutex_);

        if(count == 0)
            count++;
        else
            return;

        for(int i=0 ; i<num ; ++i)
        {
            thStk.push(grp.create_thread(boost::bind(&asio::io_service::run, &io_service)));
        }
    }
    ~ThreadPool()
    {
        io_service.stop();
        grp.join_all();
    }

    thread* getThread()
    { …
Run Code Online (Sandbox Code Playgroud)

c++ boost-asio threadpool

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

使用boost创建线程池

是否可以使用boost的线程创建线程池?我正在寻找所有的升级库,我找不到线程池管理器(或类似的东西)......有没有办法做到这一点?

TNX!

c++ boost boost-thread threadpool

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

可以使用boost :: asio :: thread_pool而不是将boost :: asio :: io_context与boost :: thread :: thread_group结合使用吗?

我正在努力消除我的一些困惑.我偶然发现了boost :: asio :: thread_pool,我认为可以使用以某种方式自动组合,boost::asio::io_context并且boost::thread::thread_group经常建议(这里或这里).似乎这个asio特定的池可以用于post任务,但另一方面,某些网络类型resolver需要传递一个对象io_context作为构造函数参数,而这些参数thread_pool不是也不是从中派生出来的.

c++ boost-asio

14
推荐指数
2
解决办法
664
查看次数

在 C++ 中,如何重用已完成执行的标准线程?

main()我的 C++ 方法中有这行代码:

std::thread foo (bar);
Run Code Online (Sandbox Code Playgroud)

这很好用。但是,我想根据外部输入随时运行相同的线程。如何重新使用此线程再次运行该线程?

我这样做的原因是我有两个需要同时运行的函数:一个是阻塞函数并接受输入 x,并以设定的时间间隔将数据输出到输出。另一个是阻塞并根据外部输入产生输出 y。这基本上应该是这样的:

int shared_x = 0;
int producer_x = 0;
int consumer_x = 0;

std::thread producer (foo);    //Modifies foo_x
std::thread consumer (bar);    //Outputs based on foo2_x
while( ;; ) {
    if(producer .join()) {
        shared_x = producer_x;
        //How should I restart the thread here?
    }
    if(consumer.join()) {
        consumer_x = shared_x;
        //Here too?
    }
}
Run Code Online (Sandbox Code Playgroud)

这似乎处理了整个线程安全问题,并允许它们同时安全地运行而无需等待。唯一的问题是我不知道如何重新启动线程。我该怎么做呢?

c++ multithreading std thread-safety

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

boost asio:针对不同任务的不同线程池

网上有很多关于创建简单线程池的例子,例如Sample1和Sample2

我想要实现的是为不同的任务创建一个单独的线程池.例如,应用程序可能有一个线程池用于处理传入的tcp连接(让我们称之为网络池),而另一个池用于与数据库(数据库池)通信.

这些传入的tcp请求可能需要来自数据库的信息.在这种情况下,它需要从数据库池中请求那些线程执行查询,并异步返回结果.

是否有推荐的方法使用boost :: asio?是否每个池都有一个io_service实例?那些线程应该如何相互通信(使用boost)?

我理解要解释所有这些,代码不会那么短而微不足道,但如果可能的话,某些伪代码将会非常受欢迎.

谢谢!

c++ multithreading boost pool boost-asio

5
推荐指数
1
解决办法
998
查看次数

在C++中调用(调用)函数的方式有多少?

我知道一种调用函数的方法:

func(x, y);
Run Code Online (Sandbox Code Playgroud)

有更多方法可以调用函数吗?

c++ function

4
推荐指数
2
解决办法
1574
查看次数

错误C2011:“ sockaddr”:“ struct”类型重新定义。参见“ sockaddr”的声明

我使用Visual Studio 2012,并尝试使静态库UDT正常工作。不幸的是,我无法编译将UDT库链接到其自身的项目,我在Windows SDK标头中遇到有关类型或宏重新定义的159个奇怪的错误。

c:\program files\windows kits\8.0\include\shared\ws2def.h(96): warning C4005: 'AF_IPX' : macro redefinition 2> c:\program files\windows kits\8.0\include\um\winsock.h(452) : see previous definition of 'AF_IPX' 2>c:\program files\windows kits\8.0\include\shared\ws2def.h(136): warning C4005: 'AF_MAX' : macro redefinition 2> c:\program files\windows kits\8.0\include\um\winsock.h(471) : see previous definition of 'AF_MAX' 2>c:\program files\windows kits\8.0\include\shared\ws2def.h(173): warning C4005: 'SO_DONTLINGER' : macro redefinition 2> c:\program files\windows kits\8.0\include\um\winsock.h(394) : see previous definition of 'SO_DONTLINGER' 2>c:\program files\windows kits\8.0\include\shared\ws2def.h(217): error C2011: 'sockaddr' : 'struct' type redefinition 2> c:\program files\windows kits\8.0\include\um\winsock.h(477) : see declaration …

c++ sockets windows static-linking visual-studio-2012

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