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 canceled 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 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
-
Mark the channel as done.
Warning
When a channel is marked as done it cannot receive or send anymore.
Throws
The following errors might be thrown:
VeniceError.canceledChannel
Thrown when the operation is performed on a canceled channel.
VeniceError.channelIsDone
Thrown when the operation is performed on an done channel.
VeniceError.unexpectedError
Thrown when an unexpected error occurs. This should never happen in the regular flow of an application.
Declaration
Swift
public func done() throws
-
Receive-only reference to an existing channel.
Example:
See morelet 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)
Declaration
Swift
public final class ReceiveOnly : Handle