[fpc-pascal] getting started with threads
Dariusz Mazur
darekm at emadar.com
Fri Sep 18 16:24:22 CEST 2009
David Emerson pisze:
> I am getting started with threads (linux/cthreads) and I'm very happy so
> far, but unsure of the best way to tackle my situation.
>
> I have a program that needs to perform about 10,000 independent tasks,
> which usually involve waiting for I/O; thus it makes sense to use
> several threads so that some waiting can be done in parallel. I thought
> I'd try it with 10 threads.
>
> When a thread completes its task, I'd like it to simply request a new
> task from the queue. The best way I could think to do this was to have
> a task assignment function (which accesses a global variable and
> returns a task id, marking it as pending.)
>
> 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. I know that this is a problem
> which has been addressed at great length in the computer science
> world ... so what's the easiest way for me to address it here and now?
>
>
> While writing this message it dawned on me that I probably need an event
> loop (I'm writing a console app) which will, e.g., sleep for 1 second
> and then check on the status of each thread's work. Then the main loop
> can assign a new task by starting a new thread, whenever it finds one
> that is finished. That puts the assignment task back in the main loop
> where there is no race condition.
>
You need FIFO queue for multiple consumer. Each task can be assigned to
them.
You can use ordinal tThreadList but it rather slow.
I use own lockfree FIFO http://www.emadar.com/fpc/lockfree.htm to
distribute task between threads
its much faster and well scaling on multicore.
--
Darek
More information about the fpc-pascal
mailing list