Sign in to follow this  
T1Oracle

Great: C++ Multithreading Tutorial

Recommended Posts

While searching the web for an explination on how to use the conditional variables from the boost threads library I ran into this. (not exactly the same source I used [different format] but all that info looks the same) - It's odd how that link mispells syncronization everywhere where it appears. US/England English thing? Not only did I find what I was looking for but the code example for the monitor pattern matched most of the code that I was writting as a concept for a thread safe queue. I did not even know what the "monitor pattern" was but it most likely will be the central block to the multithreaded approach of my game engine. Anyway, I'll probably limit most interthread communication to code very similar too
template <typename DataT>
class MonitorQueue : boost::noncopyable
{
public:
    void push(const DataT& newData)
    {
        boost::mutex::scoped_lock lock(m_monitorMutex);
        m_queue.push(newData);
        m_itemAvailable.notify_one();
    }

    DataT popWait()
    {
        boost::mutex::scoped_lock lock(m_monitorMutex);

        if(m_queue.empty())
        {
            m_itemAvailable.wait(lock);
        }

        DataT temp(m_queue.front());
        m_queue.pop();
        return temp;
    }

private:
    std::queue<DataT> m_queue;

    boost::mutex m_monitorMutex;
    boost::condition m_itemAvailable;
};

I do notice realize that the performance of copying DataT objects will be important to minimize the time waiting for locks to release. To optimize that I'll probably use boost smart pointers for most DataT objects. I hope that my plan isn't too naive but I believe that if I design the queue objects correctly and keep the number of necessary queued updates per second low, then I can reap a benefit in performance. Currently my idea is to break the threads down as such: 1) File IO 2) Rendering 3) User Input (mouse/keyboard/etc.) 4) Physics/AI/Scripts/Game Logic (may combine with 2 depending on need for synchronization)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The patterns scope lock, monitor etc are described very well in Object-Oriented Multithreading Using C++.
http://www.amazon.com/Object-Oriented-Multithreading-Using-Cameron-Hughes/dp/0471180122

Quote:
- It's odd how that link mispells syncronization everywhere where it appears. US/England English thing?

I find it strange that you change our words :)

Share this post


Link to post
Share on other sites
Quote:
Original post by T1Oracle
- It's odd how that link mispells syncronization everywhere where it appears. US/England English thing?

You actually spelled it wrong as well as the American version is synchronization.

Share this post


Link to post
Share on other sites
That is a cool tutorial, it really breaks down the necessary steps to programming multi-threaded programs. I've always known about the problems of multi-threaded apps but never known exactly how to deal with them.

Out of curiousity, how do you plan to design your input system? Will you be queueing the input from the window and then processing it in a separate thread?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this