本文整理汇总了C#中System.Threading.Barrier.AddParticipant方法的典型用法代码示例。如果您正苦于以下问题:C# Barrier.AddParticipant方法的具体用法?C# Barrier.AddParticipant怎么用?C# Barrier.AddParticipant使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.Barrier
的用法示例。
在下文中一共展示了Barrier.AddParticipant方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BarrierLimitedModeCharacterCounter
public BarrierLimitedModeCharacterCounter(ITextFile textFile, Barrier barrier)
{
this.barrier = barrier;
barrier.AddParticipant();
charCounts = new Lazy<IReadOnlyDictionary<char, int>>(() => BufferAndCount(textFile));
}
示例2: Main
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();
}
示例3: DownloadGDBFiles
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());
}
示例4: DownloadTreatment
/// <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();
}
示例5: RunSimulation
/*
* 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);
}
}
示例6: SignalSimple
public void SignalSimple ()
{
var barrier = new Barrier (0);
barrier.AddParticipant ();
Assert.IsTrue (barrier.SignalAndWait (500), "#1");
}
示例7: RunBarrierTest7_Bug603035
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;
}