public BarrierLimitedModeCharacterCounter(ITextFile textFile, Barrier barrier) { this.barrier = barrier; barrier.AddParticipant(); charCounts = new Lazy<IReadOnlyDictionary<char, int>>(() => BufferAndCount(textFile)); }
static void Main() { const int numberTasks = 2; const int partitionSize = 1000000; const int loops = 5; var taskResults = new Dictionary<int, int[][]>(); var data = new List<string>[loops]; for (int i = 0; i < loops; i++) { data[i] = new List<string>(FillData(partitionSize * numberTasks)); } var barrier = new Barrier(1); LogBarrierInformation("initial participants in barrier", barrier); for (int i = 0; i < numberTasks; i++) { barrier.AddParticipant(); int jobNumber = i; taskResults.Add(i, new int[loops][]); for (int loop = 0; loop < loops; loop++) { taskResults[i][loop] = new int[26]; } WriteLine($"Main - starting task job {jobNumber}"); Task.Run(() => CalculationInTask(jobNumber, partitionSize, barrier, data, loops, taskResults[jobNumber])); } for (int loop = 0; loop < 5; loop++) { LogBarrierInformation("main task, start signaling and wait", barrier); barrier.SignalAndWait(); LogBarrierInformation("main task waiting completed", barrier); // var resultCollection = tasks[0].Result.Zip(tasks[1].Result, (c1, c2) => c1 + c2); int[][] resultCollection1 = taskResults[0]; int[][] resultCollection2 = taskResults[1]; var resultCollection = resultCollection1[loop].Zip(resultCollection2[loop], (c1, c2) => c1 + c2); char ch = 'a'; int sum = 0; foreach (var x in resultCollection) { WriteLine($"{ch++}, count: {x}"); sum += x; } LogBarrierInformation($"main task finished loop {loop}, sum: {sum}", barrier); } WriteLine("at the end"); ReadLine(); }
private void DownloadGDBFiles() { finishFirstGDBSemaphore = new SemaphoreSlim(1); //finishFirstGDBSemaphore.Wait(); Barrier downloadTrainingBarrier = new Barrier(1); foreach (int key in _currentTraining.Playlist.Keys) { Exercise exercise = _currentTraining.Playlist[key][1]; int newKey = key; if (exercise.IsTraceable && !exercise.Downloaded) { downloadTrainingBarrier.AddParticipant(); if (newKey == FIRST_EXERCISE) { finishFirstGDBSemaphore.Wait(); } Task downloadGDB = new Task(() => DownloadCurrentGDB(exercise, newKey, downloadTrainingBarrier)); downloadGDB.Start(); } } downloadTrainingBarrier.SignalAndWait(); _currentTraining.Downloaded = true; //watch.Stop(); //var elapsedMs = watch.ElapsedMilliseconds; //Console.WriteLine("==> Downloading gdb " + elapsedMs.ToString()); }
/// <summary> /// Download training preview images for the treatment screen /// </summary> public void DownloadTreatment() { //crearte dir for user (if not already created) //string newDir = CreateDir(dir, _patient.AccountId.ToString()); //newDir = CreateDir(newDir, _patient.PatientTreatment.TreatmentNumber.ToString()); string newDir = CreateDir(dir, "treatmentThumb"); //initilaize the barrier int size = _patient.PatientTreatment.TrainingList.Count; //Barrier _barrier = new Barrier(size + 1); Barrier _barrier = new Barrier(1); for (int i = 0; i < size; i++) { int temp = i; string _from = _patient.PatientTreatment.TrainingList[temp].TrainingThumbs; string _to = newDir + "\\" + _patient.PatientTreatment.TrainingList[temp].TrainingId + ".png"; _patient.PatientTreatment.TrainingList[temp].TrainingThumbs = _to; if (!File.Exists(_to)) { _barrier.AddParticipant(); Task tempThread = new Task(() => { DownloadFile(_from, _to); _barrier.SignalAndWait(); }); tempThread.Start(); } } _barrier.SignalAndWait(); _barrier.Dispose(); }
/* * Running the simulation * */ private void RunSimulation() { int sideSize = (int)Math.Ceiling((double)_simulationField.GetLength(0) / (double)MAX_SECTOR_SIZE); int threadsCount = sideSize * sideSize; _barrier = new Barrier(0); while (true) { if (_barrier.ParticipantCount > 0) { _barrier.SignalAndWait(); } for (int sector = 0; sector <= threadsCount; sector++) { _barrier.AddParticipant(); ThreadPool.QueueUserWorkItem(new WaitCallback(new Action<object>(DoSector)), sector); } Thread.Sleep(200); } }
public void SignalSimple () { var barrier = new Barrier (0); barrier.AddParticipant (); Assert.IsTrue (barrier.SignalAndWait (500), "#1"); }
private static bool RunBarrierTest7_Bug603035() { TestHarness.TestLog("*RunBarrierTest7_Bug603035"); bool failed = false; for (int j = 0; j < 100 && !failed; j++) { Barrier b = new Barrier(0); Action[] actions = Enumerable.Repeat((Action)(() => { for (int i = 0; i < 400; i++) { try { b.AddParticipant(); b.RemoveParticipant(); } catch { failed = true; break; } } }), 4).ToArray(); Parallel.Invoke(actions); if (b.ParticipantCount != 0) failed = true; } if (failed) { TestHarness.TestLog("Bug603035 failed"); return false; } TestHarness.TestLog("Bug603035 succeeded"); return true; }