From f5ffebdf50afd9d4c59e759b194575cfc04eb4b3 Mon Sep 17 00:00:00 2001 From: Kiran Date: Fri, 11 Aug 2023 09:29:07 -0400 Subject: [PATCH] Add a `threadpool` parameter to `Channel` constructor (#50858) Without this, the task created by a `Channel` will run in the threadpool of the creating task; in the REPL, this could be the interactive threadpool. On 1.8, without threadpools: ```julia % julia +1.8 -t 8 _ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.8.5 (2023-01-08) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia> for _ in 1:10 Channel{Int}(1; spawn=true) do _ Core.print("threadid=$(Threads.threadid())\n") end end threadid=2 threadid=5 threadid=2 threadid=2 threadid=1 threadid=6 threadid=7 threadid=8 threadid=3 threadid=4 ``` On 1.9, with no interactive threads: ```julia % julia +1.9 -t 8 _ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.9.2 (2023-07-05) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia> for _ in 1:10 Channel{Int}(1; spawn=true) do _ Core.print("threadid=$(Threads.threadid())\n") end end threadid=4 threadid=4 threadid=4 threadid=2 threadid=3 threadid=1 threadid=7 threadid=5 threadid=8 threadid=6 ``` On 1.9, with an interactive thread: ```julia % julia +1.9 -t 7,1 _ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.9.2 (2023-07-05) _/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release |__/ | julia> for _ in 1:10 Channel{Int}(1; spawn=true) do _ Core.print("threadid=$(Threads.threadid())\n") end end threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 ``` With this PR, the `:default` threadpool is used instead. ```julia % julia +master -t7,1 _ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.11.0-DEV.244 (2023-08-09) _/ |\__'_|_|_|\__'_| | Commit d99f2496ff* (0 days old master) |__/ | julia> for _ in 1:10 Channel{Int}(1; spawn=true) do _ Core.print("threadid=$(Threads.threadid())\n") end end threadid=7 threadid=6 threadid=7 threadid=7 threadid=6 threadid=3 threadid=5 threadid=2 threadid=4 threadid=8 ``` And, the behavior can be overridden. ```julia % julia +master -t7,1 _ _ _ _(_)_ | Documentation: https://docs.julialang.org (_) | (_) (_) | _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help. | | | | | | |/ _` | | | | |_| | | | (_| | | Version 1.11.0-DEV.244 (2023-08-09) _/ |\__'_|_|_|\__'_| | Commit d99f2496ff* (0 days old master) |__/ | julia> for _ in 1:10 Channel{Int}(1; spawn=true, threadpool=:interactive) do _ Core.print("threadid=$(Threads.threadid())\n") end end threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 threadid=1 ``` --------- Co-authored-by: Nathan Daly