|
Boost Users : |
Subject: Re: [Boost-users] Networking TS + Beast, NEW Tutorials, Read this to learn std::net !!!
From: Stian Zeljko Vrba (vrba_at_[hidden])
Date: 2019-03-15 14:38:02
As a followup and a concrete example, here's what C++ is competing against; this piece of C# code starts an asynchronous read of a file and waits for it to complete up to some timeout value; only platform facilities are used here (i.e., no external libraries).
How does ReadAsync complete? Executor? Thread? OS-callback? Don't know, don't care, it works. I have an idea of how to accomplish the same in C++, and it's not pleasant -- worker thread, promise/future, blocking queue and CancelSynchronousIO. Cannot even use std::async because CancelSynchronousIO needs a target thread ID.
try {
int bytesRead = 0;
var operationStart = DateTime.Now;
var vt = sourceFile.ReadAsync(writeTask.Data);
if (vt.IsCompletedSuccessfully) {
bytesRead = vt.Result;
}
else {
var t = vt.AsTask();
if (!t.Wait(State.TransferTimeoutSeconds * 1000))
throw new AggregateException(new TimeoutException("Reader timed out."));
bytesRead = t.Result;
}
writeTask.Data = writeTask.Data.Slice(0, bytesRead);
var operationDuration = (DateTime.Now - operationStart);
volumeHealthReport.ReadStatistics.UpdateBandwidth(bytesRead, operationDuration);
return bytesRead;
}
catch (AggregateException e) {
if (e.InnerException != null) writeTask.ReaderException = e.InnerException;
else writeTask.ReaderException = e;
volumeHealthReport.ReadStatistics.IncrementErrorCount();
return 0;
}
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net