Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
29
Task/Metered-concurrency/C-sharp/metered-concurrency.cs
Normal file
29
Task/Metered-concurrency/C-sharp/metered-concurrency.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RosettaCode
|
||||
{
|
||||
internal sealed class Program
|
||||
{
|
||||
private static void Worker(object arg, int id)
|
||||
{
|
||||
var sem = arg as SemaphoreSlim;
|
||||
sem.Wait();
|
||||
Console.WriteLine("Thread {0} has a semaphore & is now working.", id);
|
||||
Thread.Sleep(2*1000);
|
||||
Console.WriteLine("#{0} done.", id);
|
||||
sem.Release();
|
||||
}
|
||||
|
||||
private static void Main()
|
||||
{
|
||||
var semaphore = new SemaphoreSlim(Environment.ProcessorCount*2, int.MaxValue);
|
||||
|
||||
Console.WriteLine("You have {0} processors availiabe", Environment.ProcessorCount);
|
||||
Console.WriteLine("This program will use {0} semaphores.\n", semaphore.CurrentCount);
|
||||
|
||||
Parallel.For(0, Environment.ProcessorCount*3, y => Worker(semaphore, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue