XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

cross thread communication

Last post 11/22/2009 9:18 PM by PolyVector. 1 replies.
  • 11/22/2009 8:53 PM

    cross thread communication

    Howdy,

    I'm working on a game with two asynchronous threads that are always running. One is the game another is a tracking application. The two have a small amount of information that needs to be communicated between them. I'm passing this information by having one thread directly access the objects in the other thread and visa versa. I realize this is not the correct way to handle this.

    My game runs fine except for on semi-random intervals the frame rate drops significantly. I think the problem might be related to the way I'm handling these threads. Does this seem like a possible symptom of poor multi-threading practices? If so where I can I learn about better ways to deal with this issue?

    Thanks
  • 11/22/2009 9:18 PM In reply to

    Re: cross thread communication

    There are countless ways to handle multithreaded communication, but the most basic approach is to simply lock() your resources before accessing them... If you're only dealing with reasonably small data exchanges, or performance isn't a major concern, this method can work just fine...  Otherwise, you're in for some serious research and redesigning the basics of your engine... :)

    pseudocode:

    object mySharedObject;
    thread1()
    {
        lock(mySharedObject)
        {
            //Read/Write to mySharedObject safely...
        }
    }

    thread2()
    {
        lock(mySharedObject)
        {
            //Read/Write to mySharedObject safely...
        }
    }

Page 1 of 1 (2 items) Previous Next