@@ -59,7 +59,7 @@ Channel(sz=0) = Channel{Any}(sz)
59
59
60
60
# special constructors
61
61
"""
62
- Channel{T=Any}(func::Function, size=0; taskref=nothing, spawn=false)
62
+ Channel{T=Any}(func::Function, size=0; taskref=nothing, spawn=false, threadpool=nothing )
63
63
64
64
Create a new task from `func`, bind it to a new channel of type
65
65
`T` and size `size`, and schedule the task, all in a single call.
@@ -70,9 +70,14 @@ The channel is automatically closed when the task terminates.
70
70
If you need a reference to the created task, pass a `Ref{Task}` object via
71
71
the keyword argument `taskref`.
72
72
73
- If `spawn = true`, the Task created for `func` may be scheduled on another thread
73
+ If `spawn= true`, the ` Task` created for `func` may be scheduled on another thread
74
74
in parallel, equivalent to creating a task via [`Threads.@spawn`](@ref).
75
75
76
+ If `spawn=true` and the `threadpool` argument is not set, it defaults to `:default`.
77
+
78
+ If the `threadpool` argument is set (to `:default` or `:interactive`), this implies
79
+ that `spawn=true` and the new Task is spawned to the specified threadpool.
80
+
76
81
Return a `Channel`.
77
82
78
83
# Examples
117
122
In earlier versions of Julia, Channel used keyword arguments to set `size` and `T`, but
118
123
those constructors are deprecated.
119
124
125
+ !!! compat "Julia 1.9"
126
+ The `threadpool=` argument was added in Julia 1.9.
127
+
120
128
```jldoctest
121
129
julia> chnl = Channel{Char}(1, spawn=true) do ch
122
130
for c in "hello world"
@@ -129,12 +137,18 @@ julia> String(collect(chnl))
129
137
"hello world"
130
138
```
131
139
"""
132
- function Channel {T} (func:: Function , size= 0 ; taskref= nothing , spawn= false ) where T
140
+ function Channel {T} (func:: Function , size= 0 ; taskref= nothing , spawn= false , threadpool = nothing ) where T
133
141
chnl = Channel {T} (size)
134
142
task = Task (() -> func (chnl))
143
+ if threadpool === nothing
144
+ threadpool = :default
145
+ else
146
+ spawn = true
147
+ end
135
148
task. sticky = ! spawn
136
149
bind (chnl, task)
137
150
if spawn
151
+ Threads. _spawn_set_thrpool (task, threadpool)
138
152
schedule (task) # start it on (potentially) another thread
139
153
else
140
154
yield (task) # immediately start it, yielding the current thread
@@ -149,7 +163,7 @@ Channel(func::Function, args...; kwargs...) = Channel{Any}(func, args...; kwargs
149
163
# of course not deprecated.)
150
164
# We use `nothing` default values to check which arguments were set in order to throw the
151
165
# deprecation warning if users try to use `spawn=` with `ctype=` or `csize=`.
152
- function Channel (func:: Function ; ctype= nothing , csize= nothing , taskref= nothing , spawn= nothing )
166
+ function Channel (func:: Function ; ctype= nothing , csize= nothing , taskref= nothing , spawn= nothing , threadpool = nothing )
153
167
# The spawn= keyword argument was added in Julia v1.3, and cannot be used with the
154
168
# deprecated keyword arguments `ctype=` or `csize=`.
155
169
if (ctype != = nothing || csize != = nothing ) && spawn != = nothing
@@ -159,7 +173,8 @@ function Channel(func::Function; ctype=nothing, csize=nothing, taskref=nothing,
159
173
ctype === nothing && (ctype = Any)
160
174
csize === nothing && (csize = 0 )
161
175
spawn === nothing && (spawn = false )
162
- return Channel {ctype} (func, csize; taskref= taskref, spawn= spawn)
176
+ threadpool != = nothing && (spawn = true )
177
+ return Channel {ctype} (func, csize; taskref= taskref, spawn= spawn, threadpool= threadpool)
163
178
end
164
179
165
180
closed_exception () = InvalidStateException (" Channel is closed." , :closed )
0 commit comments