MSDN中的AutoResetEvent构造函数中的示例代码

zhiheyang 2010-12-22 10:59:48
下面是msdn中AutoResetEvent构造函数中的示例代码:

using System;
using System.Threading;

namespace AutoResetEvent_Examples
{
class MyMainClass
{
//Initially not signaled.
const int numIterations = 100;
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static int number;

static void Main()
{
//Create and start the reader thread.
Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start();

for(int i = 1; i <= numIterations; i++)
{
Console.WriteLine(">>{0}", i);
number = i;

//Signal that a value has been written.
myResetEvent.Set();

//Give the Reader thread an opportunity to act.
Thread.Sleep(0);
}

//Terminate the reader thread.
myReaderThread.Abort();
}

static void MyReadThreadProc()
{
while(true)
{
//The value will not be read until the writer has written
// at least once since the last read.
myResetEvent.WaitOne();
Console.WriteLine(" <<{0}",number);
}
}
}
}

但是当我把写数据线程中的2句代码换下顺序时,就可能会出现读线程读出同一个数据的情况。
就是把

for(int i = 1; i <= numIterations; i++)
{
Console.WriteLine(">>{0}", i);
number = i;

//Signal that a value has been written.
myResetEvent.Set();

//Give the Reader thread an opportunity to act.
Thread.Sleep(0);
}



改为

for(int i = 1; i <= numIterations; i++)
{
number = i;
Console.WriteLine(">>{0}", i);
//Signal that a value has been written.
myResetEvent.Set();

//Give the Reader thread an opportunity to act.
Thread.Sleep(0);
}



时,就有可能出现读线程读出同一个数据的情况。

请问这是什么原因?
...全文
152 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
abrianna 2010-12-24
  • 打赏
  • 举报
回复
无满意答案结帖

楼主,我鄙视你到天荒地老。

对得起我给你测试不?自己不懂就否定别人回答。
zhiheyang 2010-12-22
  • 打赏
  • 举报
回复
靠,不会上图
结果如下:
<<81
>>81
<<81
zhiheyang 2010-12-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 abrianna 的回复:]

so?
你问题是什么?这个例子很简单,就是循环打印屏幕。哪来的读数据?
[/Quote]
我说的读数据就是把数据打印在屏幕上

msdn说的这个例子不会将重复的数据打印在屏幕上,但我换了那两行代码后就会出现重复的数据
abrianna 2010-12-22
  • 打赏
  • 举报
回复
so?
你问题是什么?这个例子很简单,就是循环打印屏幕。哪来的读数据?
zhiheyang 2010-12-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 abrianna 的回复:]

C# code
using System;
using System.Threading;

namespace AutoResetEvent_Examples
{
class MyMainClass
{
//Initially not signaled.
const int numIterations = 100;
static……
[/Quote]

不能这么测吧,生成的文件里写的都是写的数据,写的数据肯定没问题啊,要看的是读线程读出来的数据
abrianna 2010-12-22
  • 打赏
  • 举报
回复
using System;
using System.Threading;

namespace AutoResetEvent_Examples
{
class MyMainClass
{
//Initially not signaled.
const int numIterations = 100;
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static int number;

static void Main()
{
//Create and start the reader thread.
Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start();

for (int i = 1; i <= numIterations; i++)
{
number = i;
Console.WriteLine(">>{0}", i);
System.IO.File.AppendAllText("testlog02.txt", string.Format(">>{0}", i));


//Signal that a value has been written.
myResetEvent.Set();

//Give the Reader thread an opportunity to act.
Thread.Sleep(0);
}

//Terminate the reader thread.
myReaderThread.Abort();
Console.ReadKey();
}

static void MyReadThreadProc()
{
while (true)
{
//The value will not be read until the writer has written
// at least once since the last read.
myResetEvent.WaitOne();
Console.WriteLine(" <<{0}", number);
}
}
}
}


using System;
using System.Threading;

namespace AutoResetEvent_Examples
{
class MyMainClass
{
//Initially not signaled.
const int numIterations = 100;
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
static int number;

static void Main()
{
//Create and start the reader thread.
Thread myReaderThread = new Thread(new ThreadStart(MyReadThreadProc));
myReaderThread.Name = "ReaderThread";
myReaderThread.Start();

for (int i = 1; i <= numIterations; i++)
{
Console.WriteLine(">>{0}", i);
number = i;
System.IO.File.AppendAllText("testlog01.txt", string.Format(">>{0}", i));


//Signal that a value has been written.
myResetEvent.Set();

//Give the Reader thread an opportunity to act.
Thread.Sleep(0);
}

//Terminate the reader thread.
myReaderThread.Abort();
Console.ReadKey();
}

static void MyReadThreadProc()
{
while (true)
{
//The value will not be read until the writer has written
// at least once since the last read.
myResetEvent.WaitOne();
Console.WriteLine(" <<{0}", number);
}
}
}
}


两次,生成的内容存文件中,然后对比文件,没任何不同。
abrianna 2010-12-22
  • 打赏
  • 举报
回复
测试过。没问题。
缭绕飘渺 2010-12-22
  • 打赏
  • 举报
回复
没看出问题所在
单步跟踪下应该能够发现吧
zhiheyang 2010-12-22
  • 打赏
  • 举报
回复
半天也没人回答啊,加分
TimZhuFaith 2010-12-22
  • 打赏
  • 举报
回复
帮顶。。。我也想知道答案!可能分低了。。
zhiheyang 2010-12-22
  • 打赏
  • 举报
回复
怎么没人那

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧