我正在使用 C++ 开发一个网络程序,我想实现一个 pthread 池。每当我从接收套接字接收到一个事件时,我都会将数据放入线程池中的队列中。我正在考虑创建 5 个独立的线程,并将持续检查队列以查看是否有任何传入数据需要完成。
这是一个非常简单的话题,但我不是专家,所以我想听听任何可能有助于实现这一点的内容。
请让我知道我应该注意的任何教程、引用资料或问题。
最佳答案
使用Boost.Asio并有每个 thread在池中调用io_service::run()
。
Multiple threads may call io_service::run() to set up a pool of threads from which completion handlers may be invoked. This approach may also be used with io_service::post() to use a means to perform any computational tasks across a thread pool.
Note that all threads that have joined an io_service's pool are considered equivalent, and the io_service may distribute work across them in an arbitrary fashion.
关于c++ - 线程池,C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6366476/