[fpc-pascal] getting started with threads
David Emerson
dle3ab at angelbase.com
Fri Sep 18 03:07:48 CEST 2009
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.
Do I even need to send this message? It would be nice to get a little
input and see if I'm on the right track :)
Cheers,
David
More information about the fpc-pascal
mailing list