Parallel brute force: Difference between revisions

Content added Content deleted
Line 793: Line 793:
1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad -> zyzzx
1115dd800feaacefdf481f1f9070374a2a81e27880f187396db67958b207cbad -> zyzzx
</pre>
</pre>
Or using worker threads with buffered read/write channels
Or using worker threads with buffered read/write channels.

<p>Here all the possible test strings are batched as a stream that is fed to worker threads via a single read channel (batchChan). Each worker thread listens to the read channel (batchChan) and will write to the write channel (resultChan) when it finds a match. The worker threads loop indefinitely returning to read the next message on the read channel (batchChan). The main thread listens to the write channel (resultChan) and terminates once all three messages have been received.</p>
<lang haskell>import Control.Concurrent (forkIO, setNumCapabilities)
<lang haskell>import Control.Concurrent (forkIO, setNumCapabilities)
import Control.Concurrent.Chan (Chan, newChan, readChan, writeList2Chan)
import Control.Concurrent.Chan (Chan, newChan, readChan, writeList2Chan)