本文整理汇总了C#中Semaphore.Acquire方法的典型用法代码示例。如果您正苦于以下问题:C# Semaphore.Acquire方法的具体用法?C# Semaphore.Acquire怎么用?C# Semaphore.Acquire使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Semaphore
的用法示例。
在下文中一共展示了Semaphore.Acquire方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AquireInSameThrad
public void AquireInSameThrad()
{
Semaphore s = new Semaphore(2);
Assert.AreEqual(2, s.Permits);
s.Acquire();
s.Acquire();
Assert.AreEqual(0, s.Permits);
}
示例2: ReleaseMultipleTimesInSameThread
public void ReleaseMultipleTimesInSameThread()
{
Semaphore s = new Semaphore(2);
Assert.AreEqual(2, s.Permits);
s.Acquire();
s.Acquire();
s.Release(2);
Assert.AreEqual(2, s.Permits);
}
示例3: AttemptWithNegativeMillisInSameThread
public void AttemptWithNegativeMillisInSameThread()
{
Semaphore s = new Semaphore(2);
Assert.AreEqual(2, s.Permits);
s.Acquire();
Assert.IsTrue(s.Attempt(-400));
s.Release(2);
Assert.AreEqual(2, s.Permits);
}
示例4: Acquire
public void Acquire()
{
//lock (_lock)
//{
// --tokens;
// if (tokens >= 0) return;
//}
if (Interlocked.Decrement(ref tokens) >= 0) return;
var sem = new Semaphore();
waiting.Put(sem);
sem.Acquire();
}
示例5: UsingLikeAMutex
public void UsingLikeAMutex ()
{
Semaphore semaphore = new Semaphore (1);
Latch latch = new Latch ();
Helper helper = new Helper (semaphore, latch);
Thread thread = new Thread (new ThreadStart (helper.Go));
semaphore.Acquire ();
thread.Start ();
latch.Acquire ();
Assert.IsFalse (helper.gone);
semaphore.Release ();
thread.Join ();
Assert.IsTrue (helper.gone, "not gone");
}
示例6: BossWorkerDemo
public void BossWorkerDemo()
{
int n = 10;
Latch startPermit = new Latch();
Semaphore wait = new Semaphore(-(n - 1));
Worker [] workers = new Worker[n];
for (int i=0; i<n; ++i)
{
workers[i] = new Worker(startPermit, wait);
new Thread(new ThreadStart(workers[i].Work)).Start();
}
// very slow main initialization ...
// ... parse configuration
// ... initialize other resources used by workers
startPermit.Release();
// now it is our turn to wait for workers
wait.Acquire();
for (int i=0; i<n; ++i)
Assert.IsTrue(workers[i].worked);
}
示例7: AcquireReleaseInDifferentThreads
public void AcquireReleaseInDifferentThreads()
{
Semaphore s = new Semaphore(0);
AcquireReleaseWorker worker1 = new AcquireReleaseWorker(s);
Thread thread1 = new Thread(new ThreadStart(worker1.DoWork));
thread1.Start();
Thread.Sleep(300);
s.Release();
s.Release();
s.Acquire();
s.Acquire();
s.Release();
thread1.Join();
}
示例8: AquireReleaseInSameThread
public void AquireReleaseInSameThread()
{
Semaphore s = new Semaphore(1);
s.Acquire();
s.Release();
s.Acquire();
s.Release();
s.Acquire();
s.Release();
s.Acquire();
s.Release();
s.Acquire();
s.Release();
s.Acquire();
s.Release();
Assert.AreEqual(1, s.Permits);
}
示例9: AnInvalidApplicationIsNotUpdatedNorRemovedButWhenRemovedStillRaisesTheRemovedEvent
public void AnInvalidApplicationIsNotUpdatedNorRemovedButWhenRemovedStillRaisesTheRemovedEvent ()
{
Semaphore sync = new Semaphore (0);
AddApplication();
InitHandlerAndStartLocation (sync);
File.Delete(serviceXml);
Assert.IsFalse(sync.Attempt(1000), "some events propagated, expecting no one");
// remove
TestUtils.SafeDeleteDirectory(sampleDir);
Thread.SpinWait(1);
Assert.IsFalse(Directory.Exists(sampleDir), "directory still exists");
log.Debug("directory deleted");
sync.Acquire ();
log.Debug("sync acquired");
Assert.IsFalse (Directory.Exists (sampleDir), "directory still exists: " + sampleDir);
Assert.IsTrue (handler.applicationRemoved, "application not removed");
Assert.AreEqual (0, location.Applications.Count);
}
示例10: ReleaseMultipleBadArgument
public void ReleaseMultipleBadArgument()
{
Semaphore s = new Semaphore(2);
Assert.AreEqual(2, s.Permits);
s.Acquire();
s.Acquire();
Assert.Throws<ArgumentOutOfRangeException>(() => s.Release(-2));
}
示例11: RemoveAfterSpringAssemblyDeploy
public void RemoveAfterSpringAssemblyDeploy ()
{
Semaphore sync = new Semaphore (0);
InitHandlerAndStartLocation (sync);
AddApplication();
sync.Acquire();
string springPrivateBin = Path.Combine(sampleDir, SpringAssembliesDeployer.PrivateBinPathPrefix);
Directory.CreateDirectory(springPrivateBin);
using (File.Create(Path.Combine(springPrivateBin, "foo.dll"))) {}
Directory.Delete(sampleDir, true);
Assert.IsFalse(Directory.Exists(sampleDir), "directory still exists");
log.Debug (String.Format ("directory {0} removed to simulate application removal", sampleDir));
sync.Acquire();
Assert.IsTrue (handler.applicationRemoved, "application not removed");
Assert.AreEqual (0, location.Applications.Count, "application not removed");
}
示例12: RenamingADirectoryHostingAnApplicationItEqualsRemoveOldApplicationAndAddUnderNewName
public void RenamingADirectoryHostingAnApplicationItEqualsRemoveOldApplicationAndAddUnderNewName ()
{
Semaphore sync = new Semaphore (0);
InitHandlerAndStartLocation (sync);
AddApplication ();
sync.Acquire ();
Assert.IsTrue (handler.applicationAdded, "application not added");
Directory.Move (sampleDir, sampleDir2);
sync.Acquire (); // add or remove
sync.Acquire (); // add or remove
Assert.IsTrue (handler.applicationRemoved, "application was not removed");
}
示例13: RaiseEventRemovingAnApplication
public void RaiseEventRemovingAnApplication ()
{
AddApplication ();
Semaphore sync = new Semaphore (0);
InitHandlerAndStartLocation (sync);
Directory.Delete (sampleDir, true);
Thread.SpinWait(1);
Assert.IsFalse(Directory.Exists(sampleDir), "directory still exists");
log.Debug("directory deleted");
sync.Acquire ();
log.Debug("sync acquired");
Assert.IsFalse (Directory.Exists (sampleDir), "directory still exists: " + sampleDir);
Assert.IsTrue (handler.applicationRemoved, "application not removed");
Assert.AreEqual (0, location.Applications.Count);
}
示例14: RaiseEventAddingAnApplication
public void RaiseEventAddingAnApplication ()
{
Semaphore sync = new Semaphore (0);
InitHandlerAndStartLocation (sync);
AddApplication ();
sync.Acquire ();
Assert.IsTrue (handler.applicationAdded, "application not added");
Assert.AreEqual (1, location.Applications.Count);
}
示例15: IgnoreExceptionOnDispatcherWhenRemoving
public void IgnoreExceptionOnDispatcherWhenRemoving ()
{
factoryMock.ExpectAndReturn("CreateApplicationWatcherMonitor", watcher);
factoryMock.ExpectAndReturn("CreateApplicationWatcherMonitor", watcher);
watcherMock.Expect("StartWatching");
watcherMock.Expect("StopWatching");
watcherMock.Expect("Dispose");
watcherMock.Expect("StartWatching");
watcherMock.Expect("StopWatching");
watcherMock.Expect("Dispose");
ISync canContinue = new Semaphore(0);
ISync started = new Semaphore(0);
AddApplication();
AddApplication(sampleDir2);
IDeployEventDispatcher dispatcher = new ExceptionDispatcher(started, canContinue);
location = new FileSystemDeployLocation (dispatcher, factory, deployPath, true);
Assert.AreEqual(2, location.Applications.Count);
Directory.Delete(sampleDir, true);
started.Acquire();
canContinue.Release();
Assert.AreEqual(1, location.Applications.Count);
Directory.Delete(sampleDir2, true);
started.Acquire();
canContinue.Release();
Assert.AreEqual(0, location.Applications.Count);
}