从成员类功能创建线程

Creating a thread from a member class function

本文关键字:线程 创建 功能 成员类      更新时间:2023-10-16

这是我的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评论,其中有很大的帮助:。

您的代码有很多错误。

  1. 非静态成员特定于类。除了功能外,您还需要将类的实例传递给boost::thread的构造函数。这与线程或提升无关。

  2. 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;
}
//]