Multi-threading questions

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Multi-threading questions

      I added multi-threading to my game to put the DirectSound processing in a different thread. A global structure is used to tell the thread to play a sound.

      I set the D3DXVECTOR3 value to the position the sound will play. Then I set the bool to tell it to play the sound.

      Source Code

      1. SndStruct.Dx3[i] = Dx3;
      2. SndStruct.PlaySound[i] = true;

      The sound thread is in the following perpetual loop:

      Source Code

      1. while (!SndStruct.EXITTHREAD)
      2. {
      3. for (UINT i = 0; i < MAXOBJECTS; i++)
      4. {
      5. if (SndStruct.PlaySound[i])
      6. {
      7. SndStruct.PlaySound[i] = false;
      8. bool success = StartExplosion(pMySoundMgr, i, SndStruct.Dx3[i]);
      9. if (!success)
      10. {
      11. SndStruct.EXITTHREAD = true;
      12. break;
      13. }
      14. }
      15. if (SndStruct.PlaySound[GUNSSOUNDNUM ])
      16. {
      17. SndStruct.PlaySound[GUNSSOUNDNUM ] = false;
      18. pMySoundMgr->PlayASound GUNSSOUNDNUM , GunSIndex++);
      19. if (GunSIndex > GUNSOUNDS - 1)
      20. GunSIndex = 0;
      21. }
      22. } // End for
      23. Sleep(0);
      24. } // end while
      Display All


      My two questions are:

      1) I set the D3DXVECTOR 3 before setting the bool, assuming that all 3 floats will be set by the time the sound thread detects that the bool is true. I don't know how the hardware works on a PC. Will all three floats be set in the same memory space that the other thread will read from, or is it possible that the values will not have made it from the CPU cashe to the RAM when the other thread sees the bool set? Would it be wise to add a critical section for this?

      2) Is letting it loop with nothing more than the Sleep(0) call a good idea? Even without the sleep(0), the thread does seem to run without locking up the system. Would it be better to trigger the thread with an event? Or would the overhead on that not be worth it?

      I learned about multi-threading from the Charles Petzold book. It covers your options, but does not really explain what is best.

      Thanks.
    • RE: Multi-threading questions

      Originally posted by GamerRick
      1) I set the D3DXVECTOR 3 before setting the bool, assuming that all 3 floats will be set by the time the sound thread detects that the bool is true. I don't know how the hardware works on a PC. Will all three floats be set in the same memory space that the other thread will read from, or is it possible that the values will not have made it from the CPU cashe to the RAM when the other thread sees the bool set? Would it be wise to add a critical section for this?


      Critical sections are a good thing. Just make sure you don't deadlock. You can be certain that if you assume anything with a PC and multithreading it will bite you in the arse, so you should make every attempt to assure these values are set. You can pass data to the thread when started, or set aside a shared variable for the purpose.


      2) Is letting it loop with nothing more than the Sleep(0) call a good idea? Even without the sleep(0), the thread does seem to run without locking up the system. Would it be better to trigger the thread with an event? Or would the overhead on that not be worth it?

      I learned about multi-threading from the Charles Petzold book. It covers your options, but does not really explain what is best.

      Thanks.


      Petzold covers the basics. You should look at Win32 System Programming (Second Edition) and Multithreading Applications in Win32. The second covers all you could need. And, no - Sleep(0) is not the best way to do that. You'd want to use :

      DWORD WaitForSingleObject(HANDLE handle, DWORD milliseconds)

      Where handle is your thread handle and milliseconds is your timeout. 0 is test-and-return, and INFINITE will wait until the thread completes. If this function fails it returns WAIT_FAILED - use GetLastError() to get more info. Several objects can wait on this - as soon as the thread terminates it signals and every other object waiting for it will be awakened. See p. 58 - 74 of Multithreading Applic... for details and other options.

      I'd offer more help here, but I'm not very well versed in multithreading. I've played with it a little but I just started studies in this direction in earnest about a month back and I'm still getting my bearings.

      Rich
      "Your job is not to die for your country. Your job is to make some other poor sod die for his."

      The post was edited 1 time, last by Nebuchadnezzar ().

    • To my knowledge it doesn't. The messaging system allows the various subsystems to yield to others based on need, etc, sort of just skipping their turns as appropriate as far as I can tell.

      Personally, I would simply save things like file access, memory allocation or other non-time-critical long term tasks to separate threads, and spawn child threads to handle the task (which will become signaled to indicate completion). Things like resource loading and the like. Too many threads that absolutely must stay synchronized is a real easy way to create a monster. I don't know anything about multithreading on a mainframe, but PC's are very unpredictable when doing this. Even if you spawn 4 threads one after another in code, when they actually start is not reliable - they could start as 4, 3, 1, 2, or whatever. You can see an example of that in Multithreading.... Or I can send you some source from the examples if you like.

      Rich
      "Your job is not to die for your country. Your job is to make some other poor sod die for his."

      The post was edited 1 time, last by Nebuchadnezzar ().

    • Neb's right - there is no specific multi-threading in GameCode2 source. It doesn't need it!
      Mr.Mike
      Author, Programmer, Brewer, Patriot