Skip to content

Instantly share code, notes, and snippets.

@feanor12
Last active August 24, 2023 21:44
Show Gist options
  • Save feanor12/f9cc7f6c53a8d58724c43fb1328a2058 to your computer and use it in GitHub Desktop.
Save feanor12/f9cc7f6c53a8d58724c43fb1328a2058 to your computer and use it in GitHub Desktop.
multi target copy
using System.Threading.Channels;
Directory.CreateDirectory("tmp");
if (!File.Exists("tmp/source.a"))
{
var content = new byte[1024 * 1024 * 1024 + 10];
new Random().NextBytes(content);
File.WriteAllBytes("tmp/source.a", content);
}
var buffer = new byte[1024 * 1024];
var channel1 = Channel.CreateBounded<Memory<byte>>(5);
var channel2 = Channel.CreateBounded<Memory<byte>>(5);
var channel3 = Channel.CreateBounded<Memory<byte>>(5);
var channel4 = Channel.CreateBounded<Memory<byte>>(5);
var token = new CancellationTokenSource();
var tasks = new List<Task>();
tasks.Add(FileWriter("tmp/target.a", channel1, token.Token));
//tasks.Add(FileWriter("tmp/target.b", channel2, token.Token));
//tasks.Add(FileWriter("tmp/target.c", channel3, token.Token));
//tasks.Add(FileWriter("tmp/target.d", channel4, token.Token));
var writer1 = channel1.Writer;
var writer2 = channel2.Writer;
var writer3 = channel3.Writer;
var writer4 = channel4.Writer;
using (var streamin = File.OpenRead("tmp/source.a"))
{
while (streamin.CanRead)
{
var bytesread = await streamin.ReadAsync(buffer, 0, buffer.Length);
var buffer2 = new byte[bytesread];
buffer.AsMemory().Slice(0, bytesread).CopyTo(buffer2);
while (!writer1.TryWrite(buffer2.AsMemory())) ;
//while (!writer2.TryWrite(buffer2.AsMemory())) ;
//while (!writer3.TryWrite(buffer2.AsMemory())) ;
//while (!writer4.TryWrite(buffer2.AsMemory())) ;
if (bytesread < buffer.Length)
{
break;
}
}
}
writer1.Complete();
//writer2.Complete();
//writer3.Complete();
//writer4.Complete();
Task.WaitAll(tasks.ToArray());
async Task FileWriter(string outfile, Channel<Memory<byte>> channel, CancellationToken cancel)
{
var reader = channel.Reader;
using (var output = File.OpenWrite(outfile))
{
while (await reader.WaitToReadAsync(cancel))
{
var bytes = await reader.ReadAsync(cancel);
await output.WriteAsync(bytes);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment