0

So I want a C++ class to contain for example one public function: 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)

So generaly I need a sample like this:

#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.

How to do such thing?

2
  • please, provide functional code samples if possible.
    – Rella
    Nov 8, 2010 at 0:06
  • so, uh, your going to keep asking questions slowly adding more detail until SO community writes the app for you? Making the basic class is trivial, making it synchronize the data to get the sum you expect is difficult because you don't specify detail about what should happen if the sum being calculated should lock out changes to the numbers while it's doing the calc. Nov 8, 2010 at 3:48

1 Answer 1

-1

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;
  • 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;
    }
};
2
  • Random is ok for purposes ofexample... could I have 'int a = 0; int b = 0;' as privat class variables? please, could you provide full code sample because I am quite new to C++ and do not really get what you ment. So how to create this one big class?
    – Rella
    Nov 8, 2010 at 0:09
  • You can have a and b as private or public members of ThreadA and ThreadB respectively. If you make them private, you can add getter method to the class int getA() const {return a;}. In general is a bad advice to use global variables Nov 8, 2010 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.