Channel

public final class Channel<Type> : Handle

A channel is a synchronization primitive.

Threads

You can use Venice in multi-threaded programs. However, individual threads are strictly separated. You may think of each thread as a separate process.

In particular, a coroutine created in a thread will be executed in that same thread, and it will never migrate to a different one.

In a similar manner, a handle, such as a channel or a coroutine handle, created in one thread cannot be used in a different thread.

Example:

let channel = Channel<Int>()

let coroutine = try Coroutine {
    try channel.send(42, deadline: 1.second.fromNow())
}

let theAnswer = try channel.receive(deadline: 1.second.fromNow())
try coroutine.close()
  • Creates a channel

    Warning

    A channel is a synchronization primitive, not a container. It doesn’t store any items.

    Throws

    The following errors might be thrown:

    VeniceError.canceled

    Thrown when the operation is performed within a closed coroutine.

    VeniceError.outOfMemory

    Thrown when the system doesn’t have enough memory to perform the operation.

    VeniceError.unexpectedError

    Thrown when an unexpected error occurs. This should never happen in the regular flow of an application.

    Declaration

    Swift

    public init() throws
  • Reference to the channel which can only send.

    Declaration

    Swift

    public lazy var sendOnly: SendOnly = SendOnly(self)
  • Reference to the channel which can only receive.

    Declaration

    Swift

    public lazy var receiveOnly: ReceiveOnly = ReceiveOnly(self)
  • Sends a value to the channel.

    Declaration

    Swift

    public func send(_ value: Type, deadline: Deadline) throws
  • Sends an error to the channel.

    Declaration

    Swift

    public func send(_ error: Error, deadline: Deadline) throws
  • Receives a value from channel.

    Declaration

    Swift

    @discardableResult public func receive(deadline: Deadline) throws -> Type
  • Send-only reference to an existing channel.

    Example:

    let channel = Channel<Int>()
    
    func send(to channel: Channel<Int>.SendOnly) throws {
        try channel.send(42, deadline: 1.second.fromNow())
    }
    
    try send(to: channel.sendOnly)
    
    See more

    Declaration

    Swift

    public final class SendOnly : Handle
  • Receive-only reference to an existing channel.

    Example:

    let channel = Channel<Int>()
    
    func receive(from channel: Channel<Int>.ReceiveOnly) throws {
        let value = try channel.receive(deadline: 1.second.fromNow())
    }
    
    try receive(from: channel.receiveOnly)
    
    See more

    Declaration

    Swift

    public final class ReceiveOnly : Handle