boost :: thread在线程之间传递数据

时间:2015-06-12 17:59:24

标签: c++ multithreading class boost

我试图使用boost :: thread在我的主线程类的两个子类之间传递数据。我对多线程很新,而且有点过头了。现在代码重复产生以下内容。

Reader Api: 0

下面是我的类和子类定义。线程类是单例。

class threading
{
public:
    static threading *Instance();
    void workerFunc();
    void workerFunc2();
    void inputWorkerFunc();  // handles input processing

    void producer();
    void consumer();


    int getGlobalVariable()  // retrieves the value of globalVariable
    {
        return (globalVariable);
    } 
    void setGlobalVariable(int set) // sets the value of globalVariable 
    {
        globalVariable = set;
    }

    class Reader   
    {
        public:
        Reader(int waitTime) { _waitTime = waitTime;}
        void operator() () 
        {
            threading *thread = threading::Instance();
            int globalVariable = thread->getGlobalVariable();
            for (int i=0; i < 10; i++) 
            {
                logMsg("Reader Api: " +Ogre::StringConverter::toString(globalVariable));
//       usleep(_waitTime);
                boost::this_thread::sleep(boost::posix_time::microseconds(_waitTime));
            }
            return;
        }
    private:
        int _waitTime;
    };


    class Writer
    {
        public:
            Writer(int variable, int waitTime)
            {
                _writerVariable = variable;
                logMsg("Writer Variable: " +Ogre::StringConverter::toString(_writerVariable));

                _waitTime = waitTime;
            }
            void operator () () 
            {
                logMsg("Writer Variable: " +Ogre::StringConverter::toString(_writerVariable));

                threading *thread = threading::Instance();
                int globalVariable = thread->getGlobalVariable();
                for (int i=0; i < 10; i++) 
                {
//        usleep(_waitTime);
                    boost::this_thread::sleep(boost::posix_time::microseconds(_waitTime));
                    logMsg("Waittime Variable: " +Ogre::StringConverter::toString(_waitTime));

                    // Take lock and modify the global variable
                    boost::mutex::scoped_lock lock(_writerMutex);
                    globalVariable = _writerVariable;
                    thread->setGlobalVariable(globalVariable);

                    _writerVariable++;
                    // since we have used scoped lock, 
                    // it automatically unlocks on going out of scope
                }   
                logMsg("Writer Variable: " +Ogre::StringConverter::toString(_writerVariable));
//                    thread->setGlobalVariable(globalVariable);
            }   
        private:
            int _writerVariable;
            int _waitTime;
            static boost::mutex _writerMutex;
    };

protected:
    threading();
    threading(const threading&);
    threading& operator= (const threading&); 
private:
    static threading *pInstance;       
int globalVariable; 
    boost::mutex mutex;
    boost::condition_variable condvar;
    typedef boost::unique_lock<boost::mutex> lockType;
    double value;
    int count;
 };

以下是我在主代码中调用类的方法:

threading *thread = threading::Instance();
threading::Reader reads(100);
threading::Writer writes1(100, 200);
threading::Writer writes2(200, 200);

boost::thread readerThread(reads);
boost::thread writerThread1(writes1);
boost::this_thread::sleep(boost::posix_time::microseconds(100));

boost::thread writerThread2(writes2);

readerThread.join();
writerThread1.join();
writerThread2.join();

1 个答案:

答案 0 :(得分:1)

你显然错过了像(全局)互斥锁这样的合适的同步机制,以防止使用此代码的竞争条件:

static std::mutex globalVariableProtector;

int getGlobalVariable()  // retrieves the value of globalVariable
{
    std::lock_guard<std::mutex> lock(globalVariableProtector);
    return (globalVariable);
} 
void setGlobalVariable(int set) // sets the value of globalVariable 
{
    std::lock_guard<std::mutex> lock(globalVariableProtector);
    globalVariable = set;
}

此外,您实际应该考虑将该变量放入生产者线程的类中,并为其提供getter / setter函数,而不是使用全局变量。

我一直在分享current standard references,而不是boost。如果您有理由不能使用当前的c ++标准,Boost可能有自己lock_guardsSynchronized Data Structures的机制。