thread is not a member of std 解决方案

简单来说就算gcc4.7.1的API是 Win32 thread API.

转自stackoverflow


MinGW (which is included in TDM-GCC) comes with one of two APIs for threads: either winpthreads (based on the POSIX threads API, pthreads) or the Win32 thread API. If you have a version with the Win32 thread API, std::thread is disabled. TDM-GCC 4.7.1 uses the Win32 thread API, while TDM-GCC 4.8.1 and later come with winpthreads. That's why the compiler succeeded in one case but not the other.

This leads to 2 possible solutions for the 'thread' is not a member of 'std' problem:

  • Install a version of TDM-GCC that uses winpthreads (either standalone or with Code::Blocks). This means 4.8.1 or later.
  • Use the MinGW installer, which lets you choose which thread API to install. It also lets you choose what version of MinGW (which nowadays corresponds to the version of GCC included) to install, so you can even choose an older version such as 4.7.3, which is more stable than 4.8.1.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是因为在Boost.Asio库中没有找到`thread_pool`类的成员。`thread_pool`类在较新的Boost版本中是不再提供的。 如果你想使用线程池功能,可以考虑使用Boost.Asio库中提供的其他替代方案。例如,你可以使用`io_context`和`executor_work_guard`来实现线程池的功能。下面是一个简单的示例代码: ```cpp #include <boost/asio.hpp> #include <iostream> class ThreadPool { public: ThreadPool(size_t numThreads) : work_(ioContext_) { for (size_t i = 0; i < numThreads; ++i) { threads_.emplace_back([this]() { ioContext_.run(); }); } } ~ThreadPool() { ioContext_.stop(); for (auto& thread : threads_) { thread.join(); } } template <typename Task> void post(Task&& task) { boost::asio::post(ioContext_, std::forward<Task>(task)); } private: boost::asio::io_context ioContext_; boost::asio::executor_work_guard<boost::asio::io_context::executor_type> work_; std::vector<std::thread> threads_; }; int main() { ThreadPool threadPool(4); for (int i = 0; i < 10; ++i) { threadPool.post([i]() { std::cout << "Task " << i << " executed in thread " << std::this_thread::get_id() << std::endl; }); } std::this_thread::sleep_for(std::chrono::seconds(2)); return 0; } ``` 在这个示例中,我们使用了`io_context`和`executor_work_guard`来创建了一个简单的线程池。`post`方法用于将任务提交到线程池中执行。 请确保在编译时链接了Boost.Asio库,并将头文件路径正确地包含在编译器的搜索路径中。希望这能帮助你解决问题。如有更多疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值