[fpc-pascal] Mutithreaded book

Jorge Aldo G. de F. Junior jagfj80 at gmail.com
Mon Mar 7 23:04:01 CET 2011

I am developing an application that should scale well into those
multicore CPUs that we have today,
so, basically, i have to use threads.

Anybody knows a good book about multi-thread programming in general,
message passing etc ?

I need to know some technices to deal with some problems i am finding, for one :

1 - I use a producer-consumer model, that works ok (i have a
tobjectqueue version that is thread safe), but... what to do when more
than one consumer exists ?

I tried to pass the same object (the data) to all consumers, but now i
have a problem, each consumer is a thread, and each thread needs to
deallloc the object (i pass TObjects accross threads using the
thread-safe queue as medium) as soon as the object is not being used.

2 - How to deal with runtime reconfigurations, example :

i have a chain of producers/consumers like

receiveviaudp -> decrypt blowfish -> send to N consumers ... -> do
something with the tobject (wich is a tmemorystream)

Well, if i remove a consumer from the chain, the producer can not
detect this (it has a pointer to a consumer instance, but if i
free/dealloc the consumer, the pointer in the producer will still
"ftarget <> nil" result true (the numeric value of the pointer is
still the same, just that now it is semantically invalid))...

3 - How to wait for two signals :

Each thread must have some kind of wait, for one, the udp thread waits
for udp packets, the consumer of the udpthread (the tthread that
receives packets generated by the udp thread) waits of a TEventObject
to flag (the producer allways sets a TEventObject as soon as it places
something into the consumer queue) etc...

But this raises a problem :

How to wait for two events at the same time ? for one, how to wait for
a TEventObject.WaitFor AND for a UDP packet to arive ? (i am using
Ararat Synapse, it has a CanRead method, but i can use the socket
itself (property Socket of the TUDPBlockSocket object) for a

Basically, how to fpSelect an TEventObject ?

I need to wake-up the tthread if a Teventobject flags OR if the socket
has activity...

Well i need to know better about message-passing and consumer-producer
models in general, as i think my theory understanding is lacking...

Any good book ?

More information about the fpc-pascal mailing list