3 ms·
Those concurrency features are amazing! Are there any other languages like that?
by lemiant 11y ago
Those concurrency features are amazing! Are there any other languages like that?
- manigandham 11y agoThey seem pretty standard for today. C# is pretty great at it.
- raiph 10y agoWhat's the C# equivalent of the concurrency features listed in the OP: 1 `await` (iiuc this is fundamentally not the same as the C# feature that uses an `await` keyword) and `start`. 2 "transforming ordinary code in to concurrent code with a single method call". If you could post brief C# fragments corresponding to these two, or at least the latter (which I find the most interesting), that would be appreciated. ---- Fwiw Jonathan Worthington, arguably the lead designer of Perl 6's concurrency features (following guidance by Larry, Damian etc), spent years teaching C# parallel, async and concurrent coding before the Perl 6 features were fleshed out.
- manigandham 10y ago1) It's the same, just with finer control in C#. `await` means you're waiting for the result of a method but not blocking the thread/CPU from doing something else. These awaits use a `Task` which is a handle to get the completion status and final result. `Task` is the object returned by an async method. To start 3 methods in parallel, instead of awaiting each one, just get the task objects and wait for all at the end. var task1 = GetSomethingFromDatabaseAsync(1); var task2 = GetSomethingFromDatabaseAsync(2); var task3 = GetSomethingFromDatabaseAsync(3); Task.WaitAll(new[] { task1, task2, task3 }); 2) This is even easier and it's just talking about doing a loop over multiple threads. This can be handled by using the above style - launching a task in each iteration and waiting for them all to complete - or just using the Parallel convenience methods to launch the loop. var somethings = new List<int> { 1, 2, 3, 4, 5, 6 }; Parallel.ForEach(somethings, i => { Console.Write("Number: " + i); }); All of this stuff has been around for years with async/await baked into the language in C# v5 and the Task Parallel Library has been around even longer. The current System.Threading.Tasks namespace has a good overview of all the capabilities: https://msdn.microsoft.com/en-us/library/system.threading.tasks(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.threading.ta...
- raiph 10y ago> 1) It's the same, just with finer control in C#. `await` means you're waiting for the result of a method but not blocking the thread/CPU from doing something else. Thanks. `await` in Perl 6 is blocking. I'm guessing Task.WaitAll maybe does (some of) what `await` does in Perl 6? > 2) This is even easier and it's just talking about doing a loop over multiple threads. ... Parallel convenience methods Are you saying ordinary synchronous C# code looks like this: ForEach(somethings, i => { Console.Write("Number: " + i); }); And then, to parallelize it, folk refactor by prepending a `Parallel.`?
- manigandham 10y ago> Thanks. `await` in Perl 6 is blocking. I'm guessing Task.WaitAll maybe does (some of) what `await` does in Perl 6? Depends on what you're trying to do. `await` is just a keyword that means the CPU pauses this method until the result comes back and does some other stuff meanwhile. You can omit the keyword and method completes immediately while the other stuff gets done automatically if you dont care about the result or whether it actually finished/errored/etc. In C#, you can use Task.WaitAll() which will block the current thread until all the async tasks are finished or use Task.WhenAll() which itself is an async task that returns when all the tasks you pass in are finished (and probably the better option in actual production code). > Are you saying ordinary synchronous C# code looks like this: No, normal foreach is a language keyword/construct that just looks like: foreach(var item in someListOfItems) { DoSomething(item); } Parallel is a static class that implements parallel processing and is part of the TPL (Task Parallel Library) that has been available for years before even the async/await keywords were built-in. You can use them both in combination to manage async tasks and also do more explicit parallel threading yourself. It all depends on how much control you want and your objectives. I suggest looking at the link I posted to see some of the available options. This is a good overview to the async/await in C#: http://www.dotnetperls.com/async http://www.dotnetperls.com/async