0

I am trying to get thread synchronization to work. Below is a sample code (Runnable C++ shell example) for illustration. When I run the program, the three member functions bar1(), bar2() and bar3() are executed. My aim though is to use one thread for each of these functions. I have access to the boost library in my development environment. I know how to use threads and have also implemented a ThreadedProcess() function below. But it's not thread safe as I understand because multiple threads might want to access the same memory at the same time. So my question is, how would I ensure that the members can only be accessed by one thread at a time. Thank you!

#include <iostream>
#include <string>




   struct TestStruct {
        int nVal;
        double dVal;
        TestStruct(){}
        TestStruct(int n, double d) : nVal(n), dVal(d)
        {
        }
    };

    class Foo{

    public:

        Foo() :m_oO1(58, 17.3), m_oO2(11, 20.8), m_oO3(7, 56.3) {}

        ~Foo(){}

        void Process()
        {
            bar1();
            bar2();
            bar3();
        }

        void ThreadedProcess()
        {

            m_oThread1 = boost::thread(boost::bind(&Foo::bar1, this));
            m_oThread2 = boost::thread(boost::bind(&Foo::bar2, this));
            m_oThread3 = boost::thread(boost::bind(&Foo::bar3, this));
        }

    protected:

        TestStruct m_oO1;
        TestStruct m_oO2;
        TestStruct m_oO3;
        TestStruct m_oO4;

        boost::thread m_oThread1;
        boost::thread m_oThread2;
        boost::thread m_oThread3;

        int bar1()
        {
            int nRet = m_oO1.nVal * m_oO2.nVal;
            std::cout << nRet << std::endl;
            return nRet;
        }

        double bar2()
        {
            double dRet = m_oO2.dVal + m_oO3.dVal;
            std::cout << dRet << std::endl;
            return dRet;
        }

        double bar3()
        {
            double dRet = m_oO2.dVal * m_oO3.dVal * m_oO3.dVal;
            std::cout << dRet << std::endl;
            return dRet;
        }

    };

    int main(int /*argc*/, char** /*argv*/)
    {
        Foo myFoo;
        myFoo.Process();
        myFoo.ThreadedProcess();
    }
tzippy
  • 6,458
  • 30
  • 82
  • 151
  • What does "thread-safe" mean to you? Asking if code is thread-safe is like asking if it's secure - you should be more specific. (Although some code is obviously not thread-safe or obviously not secure) – user253751 Jul 13 '16 at 14:02
  • 1
    You are only reading in all of your threads so that is safe. You only need synchronization if you have more than one thread and at least one of them is a writer. The only thing you need to synchronize is your output. – NathanOliver Jul 13 '16 at 14:05
  • @NathanOliver Thanks for the info. So as long as the threads are only reading there's no problem as I understood. But if let's say each thread wants to access the same array, there's gonna be a problem? – tzippy Jul 13 '16 at 14:10
  • There's no need for boost here any more, assuming you've got a relatively up to date compiler. – Robinson Jul 13 '16 at 14:11
  • I am using Visual Studio 2008 compiler and don't have access to C++11 if that's what you're asking. – tzippy Jul 13 '16 at 14:12
  • @tzippy Only if they access(write to) the same element. If you can guarantee that none of could touch the same element at the same time then you are okay. That is of course if you are modify the values. You can have as many threads as you want read from the same array/array element at the same time. It is only when you start to write data that you need to make sure you do not write while someone is reading or when another writer is writing to the same thing. – NathanOliver Jul 13 '16 at 14:15
  • Also make sure your threads are cleanly terminated before exiting main() – David Thomas Jul 13 '16 at 14:37
  • @DavidThomas Yeah thanks, I forgot to join them! – tzippy Jul 13 '16 at 14:55

0 Answers0