Skip to content

Commit b04b104

Browse files
If the user explicitly asked for 1 thread don't add an interactive one. (JuliaLang#57454)
Co-authored-by: Ian Butterworth <i.r.butterworth@gmail.com>
1 parent e9e7931 commit b04b104

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

HISTORY.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ Language changes
3636
* Julia now defaults to 1 "interactive" thread, in addition to the 1 default "worker" thread. i.e. `-t1,1`.
3737
This means in default configuration the main task and repl (when in interactive mode), which both run on
3838
thread 1, now run within the `interactive` threadpool. The libuv IO loop also runs on thread 1,
39-
helping efficient utilization of the worker threadpool used by `Threads.@spawn`. Pass `0` to disable the
40-
interactive thread i.e. `-t1,0` or `JULIA_NUM_THREADS=1,0`, or `-tauto,0` etc. The zero is explicitly
41-
required to disable it, `-t2` will set the equivalent of `-t2,1` ([#57087]).
39+
helping efficient utilization of the worker threadpool used by `Threads.@spawn`. Asking for specifically 1 thread
40+
(`-t1`/`JULIA_NUM_THREADS=1`) or passing `0` will disable the interactive thread i.e. `-t1,0` or `JULIA_NUM_THREADS=1,0`
41+
, or `-tauto,0` etc. Asking for more than 1 thread will enable the interactive thread so
42+
`-t2` will set the equivalent of `-t2,1` ([#57087]).
4243
* When a method is replaced with an exactly equivalent one, the old method is not deleted. Instead, the
4344
new method takes priority and becomes more specific than the old method. Thus if the new method is deleted
4445
later, the old method will resume operating. This can be useful in mocking frameworks (as in SparseArrays,

doc/src/manual/multi-threading.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ of Julia multi-threading features.
88
By default, Julia starts up with 2 threads of execution; 1 worker thread and 1 interactive thread.
99
This can be verified by using the command [`Threads.nthreads()`](@ref):
1010

11-
```jldoctest
11+
```julia
1212
julia> Threads.nthreads(:default)
1313
1
1414
julia> Threads.nthreads(:interactive)
@@ -37,6 +37,7 @@ each threadpool.
3737

3838
!!! compat "Julia 1.12"
3939
Starting by default with 1 interactive thread, as well as the 1 worker thread, was made as such in Julia 1.12
40+
If the number of threads is set to 1 by either doing `-t1` or `JULIA_NUM_THREADS=1` an interactive thread will not be spawned.
4041

4142
Lets start Julia with 4 threads:
4243

@@ -144,6 +145,8 @@ julia> nthreads(:interactive)
144145
julia> nthreads()
145146
3
146147
```
148+
!!! note
149+
Explicitly asking for 1 thread by doing `-t1` or `JULIA_NUM_THREADS=1` does not add an interactive thread.
147150

148151
!!! note
149152
The zero-argument version of `nthreads` returns the number of threads

src/jloptions.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ JL_DLLEXPORT void jl_parse_opts(int *argcp, char ***argvp)
685685
if (nthreadsi == 0)
686686
jl_options.nthreadpools = 1;
687687
}
688+
} else if (nthreads == 1) { // User asked for 1 thread so don't add an interactive one
689+
jl_options.nthreadpools = 1;
690+
nthreadsi = 0;
688691
}
689692
jl_options.nthreads = nthreads + nthreadsi;
690693
}

src/threading.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,8 @@ void jl_init_threading(void)
740740
if (errno != 0 || endptr == cp || nthreads <= 0)
741741
nthreads = 1;
742742
cp = endptr;
743+
if (nthreads == 1) // User asked for 1 thread so lets assume they dont want an interactive thread
744+
nthreadsi = 0;
743745
}
744746
if (*cp == ',') {
745747
cp++;

test/cmdlineargs.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
348348

349349
# -t, --threads
350350
code = "print(Threads.threadpoolsize())"
351+
code2 = "print(Threads.maxthreadid())"
351352
cpu_threads = ccall(:jl_effective_threads, Int32, ())
352353
@test string(cpu_threads) ==
353354
read(`$exename --threads auto -e $code`, String) ==
@@ -358,6 +359,11 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
358359
withenv("JULIA_NUM_THREADS" => nt) do
359360
@test read(`$exename --threads=2 -e $code`, String) ==
360361
read(`$exename -t 2 -e $code`, String) == "2"
362+
if nt === nothing
363+
@test read(`$exename -e $code2`, String) == "2" #default + interactive
364+
elseif nt == "1"
365+
@test read(`$exename -e $code2`, String) == "1" #if user asks for 1 give 1
366+
end
361367
end
362368
end
363369
# We want to test oversubscription, but on manycore machines, this can

0 commit comments

Comments
 (0)