Action Engine
Loading...
Searching...
No Matches
thread::Writer< T > Class Template Reference

Detailed Description

template<class T>
class thread::Writer< T >

A Writer is used to write items to a Channel.

You can get a pointer to a Writer by calling Channel<T>::writer(), which is the object that you should pass to code that needs to write to the channel. Writers must be closed by calling Writer::Close() when the communication is done, to notify any waiting readers that no more items will be written to the channel.

#include <channel.h>

Public Member Functions

void Write (const T &item)
 Writes the given item to the channel, blocking until it can be written. If the channel is closed, this will throw an exception.
 
void Write (T &&item)
 Writes the given item to the channel, blocking until it can be written. If the channel is closed, this will throw an exception.
 
void Close ()
 Marks the channel as closed, notifying any waiting readers. See Reader::Read(). May be called at most once.
 
Case OnWrite (const T &item)
 Returns a Case that can be used to write the given item to the channel.
 
Case OnWrite (T &&item)
 Returns a Case that can be used to write the given item to the channel. The item is not mutated (moved from) unless the returned Case is selected.
 
bool WriteUnlessCancelled (const T &item)
 Returns false iff the calling fiber is cancelled before the value can be written.
 
bool WriteUnlessCancelled (T &&item)
 Returns false iff the calling fiber is cancelled before the value can be written.
 

Member Function Documentation

◆ OnWrite() [1/2]

template<typename T>
Case thread::Writer< T >::OnWrite ( const T & item)

Returns a Case that can be used to write the given item to the channel.

Note
The lifetime of the item must be guaranteed to span the call to Select() with this case. Care must be taken when passing temporaries whose lifetime may only be guaranteed for the current statement. For example:
// Legal: int(23)'s lifetime extends to end of statement.
int index = thread::Select({ c_0, c_1, writer->OnWrite(23) });
// Illegal: Lifetime of int(19) not guaranteed beyond c's
// declaration.
thread::Case c = writer->OnWrite(19);
int index = thread::Select({ c, ... });
int Select(const CaseArray &cases)
Returns the index of the first case that is ready, blocking until one is.
Definition select.h:75
A Case represents a selectable case in a Select statement.
Definition cases.h:80
Parameters
itemThe item to write to the channel. If the case is selected, ownership of the item is moved to the channel.

◆ OnWrite() [2/2]

template<typename T>
Case thread::Writer< T >::OnWrite ( T && item)

Returns a Case that can be used to write the given item to the channel. The item is not mutated (moved from) unless the returned Case is selected.

Note
The item is not mutated (moved from) unless the returned Case is selected. For example:
// This is safe, ownership is only moved from if the case is selected.
auto unique_ptr = absl::make_unique<T>();
int index = thread::Select({ c_0, c_1,
writer->OnWrite(std::move(unique_ptr)) });
if (index != 2) {
unique_ptr->Foo();
}
Parameters
itemThe item to write to the channel. If the case is selected, ownership of the item is moved to the channel.

◆ Write() [1/2]

template<typename T>
void thread::Writer< T >::Write ( const T & item)

Writes the given item to the channel, blocking until it can be written. If the channel is closed, this will throw an exception.

Note
The item is copied into the channel, so it must be copy-constructible.

◆ Write() [2/2]

template<typename T>
void thread::Writer< T >::Write ( T && item)

Writes the given item to the channel, blocking until it can be written. If the channel is closed, this will throw an exception.

Note
The item is moved into the channel, so it must be move-constructible.

◆ WriteUnlessCancelled() [1/2]

template<typename T>
bool thread::Writer< T >::WriteUnlessCancelled ( const T & item)

Returns false iff the calling fiber is cancelled before the value can be written.

Parameters
itemThe item to write to the channel.
Returns
True if the item was written to the channel, false if the calling fiber was cancelled before the item could be written.

◆ WriteUnlessCancelled() [2/2]

template<typename T>
bool thread::Writer< T >::WriteUnlessCancelled ( T && item)

Returns false iff the calling fiber is cancelled before the value can be written.

Note
The item is only moved into the channel if the function returns true.
Parameters
itemThe item to write to the channel. If the function returns true, the ownership of the item is moved to the channel.
Returns
True if the item was written to the channel, false if the calling fiber was cancelled before the item could be written.

The documentation for this class was generated from the following file: