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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Mar 27, 2017 at 18:16
\$\endgroup\$

1 Answer 1

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)

answered May 28, 2017 at 0:26
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.