[fpc-pascal] Re: getting started with threads
Tobias Giesen
tobias_subscriber at tgtools.com
Fri Sep 18 04:07:19 CEST 2009
> But how can I prevent race conditions? If threads X and Y happen to
> call the task assignment function at the same time, it seems to me
> that they could both be assigned to the same task.
Use for example the cross-platform implementations of
Enter/LeaveCriticalSection. Like this:
EnterCriticalSection(MyCritVar);
try
DoStuff
finally
LeaveCriticalSection(MyCritVar);
end;
> Then the main loop can assign a new task by starting a new thread,
> whenever it finds one > that is finished.
Also possible but not easier. Threads should not be started or
stopped for each task, instead they should wait for an event telling
them to continue with the next task. For example using SetEvent /
ResetEvent as well as:
{$ifdef win32}
WaitForSingleObject(ContinueEvent,INFINITE);
{$else}
RTLEventWaitFor(ContinueEvent);
{$endif}
Cheers,
Tobias
More information about the fpc-pascal
mailing list