Channel
Channel{T}(sz::Int)
Constructs a Channel that can hold a maximum of sz objects of type T. put! calls on a full channel block till an object is removed with take!.
Other constructors:
Channel()- equivalent toChannel{Any}(32)Channel(sz::Int)equivalent toChannel{Any}(sz)
Examples
-
Create a channel of integers with a specified size:
julia> ch = Channel{Int}(5) Channel{Int64}(sz_max:5,sz_curr:0)This example creates a channel
chthat can hold a maximum of 5 integers. -
Create a channel of strings:
julia> str_ch = Channel{String}() Channel{String}(sz_max:32,sz_curr:0)Here, we create a channel
str_chthat can hold a maximum of 32 strings. -
Put an object into the channel:
julia> put!(ch, 10)This code puts the value
10into the channelch. -
Take an object from the channel:
julia> value = take!(ch)It retrieves an object from the channel
chand assigns it to the variablevalue. -
Use
Channel()as a default constructor:julia> default_ch = Channel() Channel{Any}(sz_max:32,sz_curr:0)This creates a channel
default_chwith a default typeAnyand a maximum size of 32. - Create a channel with a specific size using
Channel(sz::Int):julia> custom_ch = Channel(10) Channel{Any}(sz_max:10,sz_curr:0)This code creates a channel
custom_chwith a maximum size of 10.
Note: The put! and take! functions are used for adding and retrieving objects from the channel, respectively. It's important to note that put! calls on a full channel will block until space becomes available by removing objects using take!.
See Also
Channel, close, put!, take!,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.