The code in the OP tries to implement threaded map. I recommend using existing solutions. For example:
- GitHub - tro3/ThreadPools.jl: Improved thread management for background and nonuniform tasks in Julia. Docs at https://tro3.github.io/ThreadPools.jl
- GitHub - mohamed82008/KissThreading.jl: Simple patterns supporting working with threads in Julia
- GitHub - baggepinnen/ThreadTools.jl: Some tools for working with threads in Julia
- GitHub - Jutho/Strided.jl: A Julia package for strided array views and efficient manipulations thereof
- GitHub - tkf/ThreadsX.jl: Parallelized Base functions
You also need to know the output type of f which is impossible in general.