help me reason about F# threads
Posted
by
Kevin Cantu
on Stack Overflow
See other posts from Stack Overflow
or by Kevin Cantu
Published on 2010-12-29T02:50:07Z
Indexed on
2010/12/29
2:53 UTC
Read the original article
Hit count: 262
In goofing around with some F# (via MonoDevelop), I have written a routine which lists files in a directory with one thread:
let rec loop (path:string) =
Array.append
(
path |> Directory.GetFiles
)
(
path
|> Directory.GetDirectories
|> Array.map loop
|> Array.concat
)
And then an asynchronous version of it:
let rec loopPar (path:string) =
Array.append
(
path |> Directory.GetFiles
)
(
let paths = path |> Directory.GetDirectories
if paths <> [||] then
[| for p in paths -> async { return (loopPar p) } |]
|> Async.Parallel
|> Async.RunSynchronously
|> Array.concat
else
[||]
)
On small directories, the asynchronous version works fine. On bigger directories (e.g. many thousands of directories and files), the asynchronous version seems to hang. What am I missing?
I know that creating thousands of threads is never going to be the most efficient solution -- I only have 8 CPUs -- but I am baffled that for larger directories the asynchronous function just doesn't respond (even after a half hour). It doesn't visibly fail, though, which baffles me. Is there a thread pool which is exhausted?
How do these threads actually work?
© Stack Overflow or respective owner