1
\$\begingroup\$

I would like to write a pseudo version of List.map with the help of the Event and Thread modules:

open Thread
open Event

let rec tmap f ls =
    let c = new_channel () in match ls with
        | [] -> []
        | h::t -> let _ = create (fun c -> sync (send c (f h :: tmap f t))) c in
            sync (receive c)

Calling the func:

# tmap (fun a -> a + a) [1;2;3;4;5;6];;
- : int list = [2; 4; 6; 8; 10; 12]

How can I make it smarter? In my case, I'm creating a thread for every list element, so if I had 10k elements this would be bad. My guess it that I should split the list, but how is it usually done?

\$\endgroup\$
2
\$\begingroup\$

An alternative would be to use a thread pool: A fixed number of thread is created and each thread pulls and execute a task

Note that threads (from the Thread module) do not run in parallel (they are time-sharing)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.