Exemple #1
0
 public SynchronizeOutput(int threadCount)
 {
     ThreadCount = threadCount;
     turnstile1  = new MySemaphore(0, 1);
     turnstile2  = new MySemaphore(1, 1);
     mutex       = new MyMutex();
     semaphores  = new MySemaphore[ThreadCount];
 }
        public void TrySchedulingAndProcessing(int maxConcurrentTasks, Action processingAction)
        {
            semaphore = new MySemaphore(maxConcurrentTasks, maxConcurrentTasks);
            List <Task> tasks = new List <Task>();

            for (int i = 0; i < TasksCount; i++)
            {
                tasks.Add(Task.Run(() => ScheduleTask(processingAction)));
            }

            Task.WaitAll(tasks.ToArray());
        }
Exemple #3
0
        /// <summary>
        /// This solution follows the approach used to solve the problem of reusable barrier mentioned in the publically available eBook - "Little Book of Semaphores".
        /// </summary>
        public void ProducePrintStream()
        {
            for (int i = 0; i < ThreadCount; i++)
            {
                semaphores[i] = new MySemaphore(0, 1);
            }

            semaphores[0].TryRelease();

            for (int i = 0; i < ThreadCount; i++)
            {
                var streamNum = i + 1;
                System.Threading.Tasks.Task.Run(() => Print(streamNum));
            }
        }