0
0

Hello,

I have got a question about thread safing and the PFSoundStream object.

I have two PFSoundStream-Variables in a class. In one function, I load and initialize the streams and create a thread (via BeginThread) and pass the two streampoitners to an array of Pointer (two elements) that is passed to the ThreadFcuntion:
[code:28nl61z6]var
SA: PStreamArray;
begin
{ stream loading and initialization }
SA[0] := FSuppStream;
SA[1] := FMainStream;
BeginThread(nil, 0, @CallbackThreadFunc, SA, 0,
ThreadID);
end;[/code:28nl61z6]
In the ThreadFunction, I poll the one stream until it is ready and then initialize the second stream and send a message to a window.
[code:28nl61z6]function CallbackThreadFunc(Param: Pointer): Integer;
begin
EnterCriticalSection(CritSect);
repeat
until FSound_Stream_GetOpenState(PStreamArray(Param)[0]) = 0;

FSound_Stream_SetPosition(PStreamArray(Param)[0], FSound_Stream_GetPosition(PStreamArray(Param)[1]));

PostMessage(Form1.Handle, FWM_FileReady, FSound_Stream_GetLengthMS(PStreamArray(Param)[0]), 0);
LeaveCriticalSection(CritSect);

EndThread(0);
end;[/code:28nl61z6]
Since I access data (the two streams) that do not belong to the thread, I need to use a critical section.
But in fact, it is not the pointer’s value (the LongWord behind it) that is changed, but the data at another address. So, I do not change data in my second thead directly.
The question is, do I really need the critical section here?
If yes, I should need it in single-thread applications, too, since there, I also use the pointer and not the data itself.

Thanks for any help!

  • You must to post comments
0
0

The whole concept of critical sections is to synchronize multiple threads acessing the same data, so yes you will need the calls to enter and leave critical sections in both threads.

I’ve got no idea if FMOD is thread-safe or not. I’ve never tried it because I avoid multi-threaded at all costs.

I really hope you are allocating memory to SA somewhere between declaring it and assigning values to its elements.

  • You must to post comments
0
0

[quote="Sly":339o94m2]The whole concept of critical sections is to synchronize multiple threads acessing the same data, so yes you will need the calls to enter and leave critical sections in both threads.[/quote:339o94m2]
That will be a lot of critical sections, but thanks :)

[quote:339o94m2]I’ve got no idea if FMOD is thread-safe or not. I’ve never tried it because I avoid multi-threaded at all costs.[/quote:339o94m2]
I try to avoid it, too, but here it can’t be avoided, or my whole application will be blocked.

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.