Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::AsyncSequence Struct Reference

Execute AsyncRequests serially, by submitting the next one after the previous one is completed. More...

#include <Async.h>

Inheritance diagram for SC::AsyncSequence:
SC::AsyncTaskSequence

Public Attributes

AsyncSequencenext = nullptr
 
AsyncSequenceprev = nullptr
 
bool clearSequenceOnCancel = true
 Do not queue next requests in the sequence when current one is cancelled.
 
bool clearSequenceOnError = true
 Do not queue next requests in the sequence when current one returns error.
 

Friends

struct AsyncEventLoop
 

Detailed Description

Execute AsyncRequests serially, by submitting the next one after the previous one is completed.

Requests are being queued on a sequence using AsyncRequest::executeOn. AsyncTaskSequence can be used to force running asyncs on a thread (useful for buffered files)

// Assuming an already created (and running) AsyncEventLoop named `eventLoop`
// ...
// Assuming an already created threadPool named `threadPool`
// ...
// Open the file (for write)
File::OpenOptions options;
options.blocking = true; // AsyncFileWrite::Task enables using regular blocking file descriptors
FileDescriptor fd;
SC_TRY(File(fd).open("MyFile.txt", File::WriteCreateTruncate, options));
// Create the async file write request
AsyncFileWrite asyncWriteFile;
asyncWriteFile.callback = [&](AsyncFileWrite::Result& res)
{
size_t writtenBytes = 0;
if(res.get(writtenBytes))
{
console.print("{} bytes have been written", writtenBytes);
}
};
// Obtain file descriptor handle
SC_TRY(fd.get(asyncWriteFile.handle, Result::Error("Invalid Handle")));
asyncWriteFile.buffer = StringView("test").toCharSpan();
// Vectorized writes: use proper start overload or set
// AsyncFileWrite::buffers and AsyncFileWrite::singleBuffer = false
// Start the operation in a thread pool
AsyncTaskSequence asyncFileTask;
SC_TRY(asyncWriteFile.executeOn(asyncFileTask, threadPool));
SC_TRY(asyncWriteFile.start(eventLoop));
// Execute another file write AFTER the previous one is finished on the same threadPool
AsyncFileWrite asyncWriteFileLater;
asyncWriteFileLater.buffer = StringView("AFTER").toCharSpan();
SC_TRY(asyncWriteFileLater.executeOn(asyncFileTask, threadPool));
SC_TRY(asyncWriteFile.start(eventLoop));
// Alternatively if the file is opened with blocking == false, AsyncFileRead can be omitted
// but the operation will not be fully async on regular (buffered) files, except on io_uring.
//
// SC_TRY(asyncWriteFile.start(eventLoop));

Member Data Documentation

◆ clearSequenceOnCancel

bool SC::AsyncSequence::clearSequenceOnCancel = true

Do not queue next requests in the sequence when current one is cancelled.

◆ clearSequenceOnError

bool SC::AsyncSequence::clearSequenceOnError = true

Do not queue next requests in the sequence when current one returns error.


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