从成员类功能创建线程
Creating a thread from a member class function
这是我的class.h
class threads_queue{
private:
boost::condition_variable the_condition_variable;
public:
//boost::atomic<bool> done;
boost::lockfree::spsc_queue<std::pair<char, std::string>> q{100};
//threads_queue() : done(false) {};
void static run_function();
void add_query(std::string, std::string);
void get_query(void);
};
&amp;这是class.cpp
void threads_queue::get_query(void){
std::pair<char, std::string> value;
//to do..
}
void threads_queue::add_query(std::string str, std::string work){
//to do ..
}
void run_function(){
//Here I want to create two threads
//First thread like
boost::thread producer_thread(add_query);
boost::thread consumer_thread(get_query);
producer_thread.join();
//done = true;
consumer_thread.join()
}
我正在遵循此示例:http://www.boost.org/doc/libs/1_54_0/doc/html/lockfree/examples.html
但是问题是当我想创建一个线程时,我总是会遇到错误,它不起作用
这是我解决错误的尝试:
1.
boost::thread consumer_thread(&threads_queue::get_query);
我有一个错误:
称为对象类型'void(threads_queue ::*)()不是函数或 功能指针
2。
boost::thread consumer_thread(&threads_queue::get_query, this);
我有一个错误:
非静态成员函数之外的"此"的使用无效
3。
boost::thread* thr = new boost::thread(boost::bind(&threads_queue::get_query));
我有一个错误:
/usr/local/include/boost/bind/bind.hpp:hpp:75:22:键入'void (threads_queue ::*)()'在'::'之前不能使用,因为它没有 成员
我不是如何解决这个问题,有任何帮助吗?
更新该主题对这个问题有很好的讨论:使用Boost线程和非静态类功能
我的主要问题是我忘了添加
threads_queue::
在我的CPP文件中的运行()之前,下面有Mikhail评论,其中有很大的帮助:。
您的代码有很多错误。
-
非静态成员特定于类。除了功能外,您还需要将类的实例传递给
boost::thread
的构造函数。这与线程或提升无关。 -
threads_queue
应该"拥有"线程,并且可能应重命名为类似线程容器的东西。整个班级应最小化。
这是我以外的其他人写的完整示例。
/ Copyright (C) 2009 Tim Blechmann
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//[spsc_queue_example
#include <boost/thread/thread.hpp>
#include <boost/lockfree/spsc_queue.hpp>
#include <iostream>
#include <boost/atomic.hpp>
int producer_count = 0;
boost::atomic_int consumer_count (0);
boost::lockfree::spsc_queue<int, boost::lockfree::capacity<1024> > spsc_queue;
const int iterations = 10000000;
void producer(void)
{
for (int i = 0; i != iterations; ++i) {
int value = ++producer_count;
while (!spsc_queue.push(value))
;
}
}
boost::atomic<bool> done (false);
void consumer(void)
{
int value;
while (!done) {
while (spsc_queue.pop(value))
++consumer_count;
}
while (spsc_queue.pop(value))
++consumer_count;
}
int main(int argc, char* argv[])
{
using namespace std;
cout << "boost::lockfree::queue is ";
if (!spsc_queue.is_lock_free())
cout << "not ";
cout << "lockfree" << endl;
boost::thread producer_thread(producer);
boost::thread consumer_thread(consumer);
producer_thread.join();
done = true;
consumer_thread.join();
cout << "produced " << producer_count << " objects." << endl;
cout << "consumed " << consumer_count << " objects." << endl;
}
//]
相关文章:
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- C++ 线程创建/删除与线程停止/恢复
- Qt - 如何从线程创建 QFuture
- 我可以使用Qt线程ID为每个线程创建唯一的缓存吗?
- 零MQ 后台线程创建
- OpenMP 线程创建
- GLFW & ImGui:从 main 以外的线程创建 ImGui 控件
- 对象:无法为位于不同线程中的父线程创建子级
- C++ 11:线程创建给我一个"Attempt to use a deleted function"错误
- C 的周期性线程创建
- MPI - 当数组初始化值必须为常量时,如何为工作线程创建部分数组
- 当主GUI线程被阻塞时,如何从工作线程创建无模式对话框
- 多个线程创建5个线程来计算质数
- 为线程创建模板
- 线程创建,CRT和DLL是如何完成的?
- 同步线程创建和销毁(静态)对象
- 竞争条件:一个线程创建静态对象,另一个线程在初始化完成之前使用它.如何处理
- 从不同线程创建QMainWindow
- QFuture 无法为位于不同线程中的父线程创建子级
- ( QNativeSocketEngine)QObject:无法为位于不同线程中的父线程创建子级