E*******s 发帖数: 42 | 1 what is their difference? Below is a little program I wrote to test
multithreading. When I use WaitOne(), it can not work properly. when I switch
to WaitOne(0, true), I got expected result. I read the manual, but still not
100% sure what their difference really is? Anyone, any idea?
You can just cut-paste this one, compile, run this small program.
Happy holidays, everyone.
====
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms; |
|
G***o 发帖数: 5158 | 2 gerdo deserves a spank for
the shameless ripoff from newsgroups:
====================================
The second argument only matters if you have a lock on
the block of code that contains the Mutex.WaitOne(,) call.
Passing true will cause the thread to release its lock on
that code, then sleep for the designated amount of time,
then reaquire the lock before continuing.
Passing false will cause the thread to keep its lock on
that code while it sleeps so it doesn't have to reaquire
it when it wak |
|
k****i 发帖数: 1072 | 3 WaitOne returns true if the event is signalled(or the thread is blocked
infinitely),false if the timeout occured.So when you specified the timeout to
zero,it return false immediately;If there is no parameter,the thread is just
blocked.
not |
|
h******e 发帖数: 52 | 4 来自主题: JobHunting版 - g 家面经 producer / consumer 问题, 要求threadsafe, high throughput
class ProducerConsumer
{
ReaderWriterLock rwLock = new ReaderWriterLock();
AutoResetEvent FullEvent = new AutoResetEvent ();
AutoResetEvent EmptyEvent = new AutoResetEvent ();
public void Producer()
{
rwLock.AcquireWriterLock();
while(queue is full)
{
FullEvent.waitOne();
}
//add
if(Queue.count == 1)
... 阅读全帖 |
|
h******e 发帖数: 52 | 5 来自主题: JobHunting版 - g 家面经 简化一下
public class ProducerConsumer
{
private Semaphore fillCount;
private Semaphore emptyCount;
private ConcurrentQueue queue;
public ProducerConsumer()
{
fillCount = new Semaphore(0, 100);
emptyCount = new Semaphore(100, 100);
queue = ConcurrentQueue();
}
public void Producer()
{
while(true)
{
//produce something
int x = 1;
... 阅读全帖 |
|
E*******s 发帖数: 42 | 6 我把原程序抄过来, 有劳大家只用抄过去, run 一 run, 讲一讲想法啊:) 万能的坛子.
switch |
|