【问题标题】:C++ Boost multithreading inside a class类中的 C++ Boost 多线程
【发布时间】:2018-03-12 06:46:53
【问题描述】:

我正在关注 Boost 多线程教程here .在第 18.13 节之后,我尝试创建一个包含多个线程的类,如下所示:

#define _CRT_SECURE_NO_WARNINGS
#include <ctime>
#include <iostream>
#include <string>
#include <queue>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/thread.hpp>
#include <boost/thread/thread.hpp> 
#include <boost/chrono.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using boost::asio::ip::udp;
using std::cout;
using std::cin;
using std::endl;
using std::string;
using namespace std;

class MultiTask
{
private:
    boost::thread_group threads;    // thread group
    boost::thread* thread_main;     // main thread
    boost::thread* thread_output;   // output thread
    boost::thread* thread_input;    // input thread
    boost::mutex stopMutex;
    bool stop;
    int i_in, i_out, i_main;
    string userInput;
public:
    // constructor
    MultiTask()
    {
        thread_main = new boost::thread(boost::ref(*this));
        thread_output = new boost::thread(&MultiTask::Callable_Out, this, 1000, boost::ref(i_out));
        thread_input = new boost::thread(&MultiTask::Callable_In, this, 1000, boost::ref(i_out), boost::ref(userInput));
        //threads.add_thread(thread_main);  // main thread = 0 // will throw -> boost thread: trying to join itself
        threads.add_thread(thread_output);  // output thread = 1
        threads.add_thread(thread_input);   // input thread = 2
        stop = false;
        i_in = 0;
        i_out = 0;
        i_main = 0;
        userInput = "";
    }
    // destructor
    ~MultiTask()
    {
        // stop all threads
        Stop();

        // show exit message
        cout << "Exiting MultiTask." << endl;
    }

    // start the threads
    void Start()
    {
        // Wait till they are finished
        threads.join_all();
    }

    // stop the threads
    void Stop()
    {
        // warning message
        cout << "Stopping all threads." << endl;

        // signal the threads to stop (thread-safe)
        stopMutex.lock();
        stop = true;
        stopMutex.unlock();

        // wait for the threads to finish
        threads.interrupt_all();
        threads.join_all();
    }

    void Callable_Out(int interval, int& count)
    {
        while (1)
        {
            //cout << "Callable_Out [" << count++ << "]" << endl;
            boost::this_thread::sleep(boost::posix_time::millisec(interval));
            boost::this_thread::interruption_point();
        }
    }

    void Callable_In(int interval, int& count, string& userInput)
    {
        while (1)
        {
            cout << "Callable_In [" << count++ << "]. Enter message: ";
            getline(cin, userInput);
            boost::this_thread::sleep(boost::posix_time::millisec(interval));
            boost::this_thread::interruption_point();
        }
    }

    // Thread function
    void operator () ()
    {
        while (1)
        {
            //cout << "Main [" << i_main++ << "]." << endl;
            //cout << "Main [" << i_main++ << "]. " << userInput << endl;
            if (userInput == "STOP")
            {
                try
                {
                    this->Stop();
                }
                catch(exception e)
                {
                    cout << e.what() << endl;
                }
            }

            boost::this_thread::sleep(boost::posix_time::millisec(1000));
            boost::this_thread::interruption_point();
        }
    }
};

int main()
{
    MultiTask mt;
    mt.Start();
}

但是,VS 会抛出以下两个错误:

严重性代码描述项目文件行抑制状态 错误 C2198 'void (__cdecl *)(boost::posix_time::millisec,int &,std::string &)': call mycpp c:\boost_1_66_0\boost\bind\bind.hpp 259 的参数太少

有人可以帮忙吗?这来自第 18.13 节。此外,在该示例中,我看不到在哪里输入 CallableFunction() 的参数。在我的情况下怎么做?谢谢。

【问题讨论】:

    标签: c++ multithreading boost


    【解决方案1】:

    教程中CallableFunction函数只有一个参数,在线程构造函数new boost::thread(&amp;CallableFunction, i);中作为第二个参数传递。

    在您的情况下,Callable_Out 有 2 个参数,缺少一个,您应该调用

    thread_output = new boost::thread(&Callable_Out, boost::posix_time::millisec(0), boost::ref(i_out));
    

    你打电话给Callable_In

    thread_input = new boost::thread(&Callable_In, boost::posix_time::millisec(1), boost::ref(i_out), boost::ref(userInput));
    

    【讨论】:

    • 那么 Callable 函数的参数输入权在声明中吗?有这方面的文件吗?非常感谢。
    • 您使用boost::thread,所以最好的参考是boost.org/doc/libs/1_66_0/doc/html/thread/…,在启动线程部分描述了如何将参数传递给线程构造函数对象。
    • 如果我想把 Callable_Out() 和 Callable_In() 放在我的班级里,我的线程声明应该如何?
    • @CaTx 只需将Callable_Out 和Callable_In 的定义复制并粘贴到MultiTask 类定义中。 - class MultiTask { .... ~MultiTask() { ... } void Callable_Out (...) {} 但是你必须改变 thread 的构造函数,现在线程的主体是类的函数成员(不是函数)所以你应该把 this 放在指向成员的指针之后(函数成员被执行对于此指针指向的对象),所以new boost::thread(&amp;Callable_Out, this, boost::posix_time::millisec(0), boost::ref(i_out)); 并以同样的方式为Callable_In 方法构造线程。
    • @CaTx 你用的是什么编译器?试试这个new boost::thread(&amp;MultiTask::Callable_Out, this, boost::posix_time::millisec(0), boost::ref(i_out))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多