Jump to content

Metered concurrency: Difference between revisions

({{omit from|ZX Spectrum Basic}})
(→‎{{header|C}}: Adding C#)
Line 211:
pthread_join(workers[i], NULL);
}
}</lang>
=={{header|C sharp|C#}}==
C# has built in semaphore system where acquire is called via Wait(), release with Release() and count with semaphore.CurrentCount.
<lang csharp>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));
}
}
}</lang>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.