简体   繁体   English

如何创建一个内部有2个线程以及不在该线程中的公共方法数组的类? (C ++,使用Boost库)

[英]How to create a class that would have a 2 threads inside and array of public methods that are not in that threads? (C++, using Boost librarys)

So I want a C++ class to contain for example one public function: int summ(); 因此,我希望C ++类包含一个公共函数,例如: int summ(); that would return an int which would be created as sum of 2 variables (each of that variabes should be edited by one thread) 这将返回一个int ,该int将创建为2个变量的总和(每个变量应由一个线程编辑)

So generaly I need a sample like this : 所以generaly我需要这样一个样本这样

#include <iostream>
#include <boost/thread.hpp>

namespace this_thread = boost::this_thread;

int a = 0;
int b = 0;
int c = 0;

class BaseThread
{
public:
    BaseThread()
        { }
    virtual ~BaseThread()
        { }

    void operator()()
    {
        try
        {
            for (;;)
            {
                // Check if the thread should be interrupted
                this_thread::interruption_point();

                DoStuff();
            }
        }
        catch (boost::thread_interrupted)
        {
            // Thread end
        }
    }

protected:
    virtual void DoStuff() = 0;
};

class ThreadA : public BaseThread
{
protected:
    virtual void DoStuff()
    {
        a += 1000;
        // Sleep a little while (0.5 second)
        this_thread::sleep(boost::posix_time::milliseconds(500));
    }
};

class ThreadB : public BaseThread
{
protected:
    virtual void DoStuff()
    {
        b++;
        // Sleep a little while (0.5 second)
        this_thread::sleep(boost::posix_time::milliseconds(100));
    }
};

int main()
{
    ThreadA thread_a_instance;
    ThreadB thread_b_instance;

    boost::thread threadA = boost::thread(thread_a_instance);
    boost::thread threadB = boost::thread(thread_b_instance);

    // Do this for 10 seconds (0.25 seconds * 40 = 10 seconds)
    for (int i = 0; i < 40; i++)
    {
        c = a + b;
        std::cout << c << std::endl;

        // Sleep a little while (0.25 second)
        this_thread::sleep(boost::posix_time::milliseconds(250));
    }

    threadB.interrupt();
    threadB.join();

    threadA.interrupt();
    threadA.join();
}

but sum should be not in main programm but in some class that our main programm would be capable to create and get that summ() when needed. 但是sum不应在主程序中,而应在主程序可以在需要时创建并获取该summ()某个类中。

How to do such thing? 怎么做这样的事情?

The example is a little strange, as the results would be random, depending on the exact coordination of the threads and sleep times. 该示例有点奇怪,因为结果是随机的,具体取决于线程和睡眠时间的确切协调。 However, to get what you want, you have several options. 但是,要获得所需的内容,您有几种选择。 You could create a new class with: 您可以使用以下方法创建一个新类:

  • The code of your main function as a class method; 您的主要功能代码为类方法;
  • The c (sum) variable would be a class member; c (sum)变量将是一个类成员;
  • The code of the method would return the sum. 该方法的代码将返回总和。

Something like: 就像是:

class TwoThreads
{
    int sum;

public:
    int doSomething()
    {
        // Same code as in main, using "sum" as the accumulator variable
        return sum;
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM