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 to Channel{Any}(32)
  • Channel(sz::Int) equivalent to Channel{Any}(sz)

Examples

  1. 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 ch that can hold a maximum of 5 integers.

  2. Create a channel of strings:

    julia> str_ch = Channel{String}()
    Channel{String}(sz_max:32,sz_curr:0)

    Here, we create a channel str_ch that can hold a maximum of 32 strings.

  3. Put an object into the channel:

    julia> put!(ch, 10)

    This code puts the value 10 into the channel ch.

  4. Take an object from the channel:

    julia> value = take!(ch)

    It retrieves an object from the channel ch and assigns it to the variable value.

  5. Use Channel() as a default constructor:

    julia> default_ch = Channel()
    Channel{Any}(sz_max:32,sz_curr:0)

    This creates a channel default_ch with a default type Any and a maximum size of 32.

  6. 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_ch with 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.

*Required Field
Details

Checking you are not a robot: