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.
The sound thread is in the following perpetual loop:
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.
I set the D3DXVECTOR3 value to the position the sound will play. Then I set the bool to tell it to play the sound.
The sound thread is in the following perpetual loop:
Source Code
- while (!SndStruct.EXITTHREAD)
- {
- for (UINT i = 0; i < MAXOBJECTS; i++)
- {
- if (SndStruct.PlaySound[i])
- {
- SndStruct.PlaySound[i] = false;
- bool success = StartExplosion(pMySoundMgr, i, SndStruct.Dx3[i]);
- if (!success)
- {
- SndStruct.EXITTHREAD = true;
- break;
- }
- }
- if (SndStruct.PlaySound[GUNSSOUNDNUM ])
- {
- SndStruct.PlaySound[GUNSSOUNDNUM ] = false;
- pMySoundMgr->PlayASound GUNSSOUNDNUM , GunSIndex++);
- if (GunSIndex > GUNSOUNDS - 1)
- GunSIndex = 0;
- }
- } // End for
- Sleep(0);
- } // end while
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.