几何尺寸与公差论坛

 找回密码
 注册
查看: 2468|回复: 2

【转帖】c#线程

[复制链接]
发表于 2007-8-1 13:54:41 | 显示全部楼层 |阅读模式
1)线程简介
单个写入程序/多个阅读程序在.Net类库中其实已经提供了实现,即System.Threading.ReaderWriterLock类。本文通过对常见的单个写入/多个阅读程序的分析来探索c#的多线程编程。


问题的提出
所谓单个写入程序/多个阅读程序的线程同步问题,是指任意数量的线程访问共享资源时,写入程序(线程)需要修改共享资源,而阅读程序(线程)需要读取数据。在这个同步问题中,很容易得到下面二个要求:
1) 当一个线程正在写入数据时,其他线程不能写,也不能读。
2) 当一个线程正在读入数据时,其他线程不能写,但能够读。
在数据库应用程序环境中经常遇到这样的问题。比如说,有n个最终用户,他们都要同时访问同一个数据库。其中有m个用户要将数据存入数据库,n-m个用户要读取数据库中的记录。
很显然,在这个环境中,我们不能让两个或两个以上的用户同时更新同一条记录,如果两个或两个以上的用户都试图同时修改同一记录,那么该记录中的信息就会被破坏。
我们也不让一个用户更新数据库记录的同时,让另一用户读取记录的内容。因为读取的记录很有可能同时包含了更新和没有更新的信息,也就是说这条记录是无效的记录。
实现分析
规定任一线程要对资源进行写或读操作前必须申请锁。根据操作的不同,分为阅读锁和写入锁,操作完成之后应释放相应的锁。将单个写入程序/多个阅读程序的要求改变一下,可以得到如下的形式:
一个线程申请阅读锁的成功条件是:当前没有活动的写入线程。
一个线程申请写入锁的成功条件是:当前没有任何活动(对锁而言)的线程。
因此,为了标志是否有活动的线程,以及是写入还是阅读线程,引入一个变量m_nActive,如果m_nActive > 0,则表示当前活动阅读线程的数目,如果m_nActive=0,则表示没有任何活动线程,m_nActive <0,表示当前有写入线程在活动,注意m_nActive<0,时只能取-1的值,因为只允许有一个写入线程活动。
为了判断当前活动线程拥有的锁的类型,我们采用了线程局部存储技术(请参阅其它参考书籍),将线程与特殊标志位关联起来。
申请阅读锁的函数原型为:public void AcquireReaderLock( int millisecondsTimeout ),其中的参数为线程等待调度的时间。函数定义如下:
public void AcquireReaderLock( int millisecondsTimeout )
{
// m_mutext很快可以得到,以便进入临界区
m_mutex.WaitOne( );
// 是否有写入线程存在
bool bExistingWriter = ( m_nActive < 0 );
if( bExistingWriter )
{ //等待阅读线程数目加1,当有锁释放时,根据此数目来调度线程
m_nWaitingReaders++;
}
else
{ //当前活动线程加1
m_nActive++;
}
m_mutex.ReleaseMutex();
//存储锁标志为Reader
System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot(m_strThreadSlotName);
object obj = Thread.GetData( slot );
LockFlags flag = LockFlags.None;
if( obj != null )
flag = (LockFlags)obj ;
if( flag == LockFlags.None )
{
Thread.SetData( slot, LockFlags.Reader );
}
else
{
Thread.SetData( slot, (LockFlags)((int)flag | (int)LockFlags.Reader ) );
}

if( bExistingWriter )
{ //等待指定的时间
this.m_aeReaders.WaitOne( millisecondsTimeout, true );
}
}
它首先进入临界区(用以在多线程环境下保证活动线程数目的操作的正确性)判断当前活动线程的数目,如果有写线程(m_nActive<0)存在,则等待指定的时间并且等待的阅读线程数目加1。如果当前活动线程是读线程(m_nActive>=0),则可以让读线程继续运行。
申请写入锁的函数原型为:public void AcquireWriterLock( int millisecondsTimeout ),其中的参数为等待调度的时间。函数定义如下:
public void AcquireWriterLock( int millisecondsTimeout )
{
// m_mutext很快可以得到,以便进入临界区
m_mutex.WaitOne( );
// 是否有活动线程存在
bool bNoActive = m_nActive == 0;
if( !bNoActive )
{
m_nWaitingWriters++;
}
else
{
m_nActive--;
}
m_mutex.ReleaseMutex();
//存储线程锁标志
System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot( "myReaderWriterLockDataSlot" );
object obj = Thread.GetData( slot );
LockFlags flag = LockFlags.None;
if( obj != null )
flag = (LockFlags)Thread.GetData( slot );
if( flag == LockFlags.None )
{
Thread.SetData( slot, LockFlags.Writer );
}
else
{
Thread.SetData( slot, (LockFlags)((int)flag | (int)LockFlags.Writer ) );
}
//如果有活动线程,等待指定的时间
if( !bNoActive )
this.m_aeWriters.WaitOne( millisecondsTimeout, true );
}
它首先进入临界区判断当前活动线程的数目,如果当前有活动线程存在,不管是写线程还是读线程(m_nActive),线程将等待指定的时间并且等待的写入线程数目加1,否则线程拥有写的权限。
释放阅读锁的函数原型为:public void ReleaseReaderLock()。函数定义如下:
public void ReleaseReaderLock()
{
System.LocalDataStoreSlot slot = Thread.GetNamedDataSlot(m_strThreadSlotName );
LockFlags flag = (LockFlags)Thread.GetData( slot );
if( flag == LockFlags.None )
{
return;
}
bool bReader = true;
switch( flag )
{
case LockFlags.None:
break;
case LockFlags.Writer:
bReader = false;
break;
}
if( !bReader )
return;
Thread.SetData( slot, LockFlags.None );
m_mutex.WaitOne();
AutoResetEvent autoresetevent = null;
this.m_nActive --;
if( this.m_nActive == 0 )
{
if( this.m_nWaitingReaders > 0 )
{
m_nActive ++ ;
m_nWaitingReaders --;
autoresetevent = this.m_aeReaders;
}
else if( this.m_nWaitingWriters > 0)
{
m_nWaitingWriters--;
m_nActive --;
autoresetevent = this.m_aeWriters ;
}
}
m_mutex.ReleaseMutex();
if( autoresetevent != null )
autoresetevent.Set();
}

释放阅读锁时,首先判断当前线程是否拥有阅读锁(通过线程局部存储的标志),然后判断是否有等待的阅读线程,如果有,先将当前活动线程加1,等待阅读线程数目减1,然后置事件为有信号。如果没有等待的阅读线程,判断是否有等待的写入线程,如果有则活动线程数目减1,等待的写入线程数目减1。释放写入锁与释放阅读锁的过程基本一致,可以参看源代码。
注意在程序中,释放锁时,只会唤醒一个阅读程序,这是因为使用AutoResetEvent的原历,读者可自行将其改成ManualResetEvent,同时唤醒多个阅读程序,此时应令m_nActive等于整个等待的阅读线程数目。
测试程序取自.Net FrameSDK中的一个例子,只是稍做修改。测试程序如下,
using System;
using System.Threading;
using MyThreading;
class Resource {
myReaderWriterLock rwl = new myReaderWriterLock();
public void Read(Int32 threadNum) {
rwl.AcquireReaderLock(Timeout.Infinite);
try {
Console.WriteLine("Start Resource reading (Thread={0})", threadNum);
Thread.Sleep(250);
Console.WriteLine("Stop Resource reading (Thread={0})", threadNum);
}
finally {
rwl.ReleaseReaderLock();
}
}
public void Write(Int32 threadNum) {
rwl.AcquireWriterLock(Timeout.Infinite);
try {
Console.WriteLine("Start Resource writing (Thread={0})", threadNum);
Thread.Sleep(750);
Console.WriteLine("Stop Resource writing (Thread={0})", threadNum);
}
finally {
rwl.ReleaseWriterLock();
}
}
}
class App {
static Int32 numAsyncOps = 20;
static AutoResetEvent asyncOpsAreDone = new AutoResetEvent(false);
static Resource res = new Resource();

public static void Main() {
for (Int32 threadNum = 0; threadNum < 20; threadNum++) {
ThreadPool.QueueUserWorkItem(new WaitCallback(UpdateResource), threadNum);
}
asyncOpsAreDone.WaitOne();
Console.WriteLine("All operations have completed.");
Console.ReadLine();
}
// The callback method's signature MUST match that of a System.Threading.TimerCallback
// delegate (it takes an Object parameter and returns void)
static void UpdateResource(Object state) {
Int32 threadNum = (Int32) state;
if ((threadNum % 2) != 0) res.Read(threadNum);
else res.Write(threadNum);
if (Interlocked.Decrement(ref numAsyncOps) == 0)
asyncOpsAreDone.Set();
}
}
从测试结果中可以看出,可以满足单个写入程序\多个阅读程序的实现要求。
 楼主| 发表于 2007-8-1 13:59:24 | 显示全部楼层

回复: 【转帖】c#线程

2)简单example
使用线程创建 Visual C# .NET 应用程序
启动 Microsoft Visual Studio .NET。
新建名为 ThreadWinApp 的 Visual C# .NET Windows 应用程序项目。
向窗体添加一个“Button”控件。默认情况下,该按钮名为“Button1”。
向窗体添加一个“ProgressBar”组件。默认情况下,该进度栏名为“ProgressBar1”。
右键单击该窗体,然后单击“查看代码”。
将以下语句添加到文件的开头:using System.Threading;
为“Button1”添加以下“Click”事件处理程序:private void button1_Click(object sender, System.EventArgs e)
{
MessageBox.Show("This is the main thread");
}
将下面的变量添加到 Form1 类:private Thread trd;
将下面的方法添加到 Form1 类:private void ThreadTask()
{
int stp;
int newval;
Random rnd=new Random();
while(true)
{
stp=this.progressBar1.Step*rnd.Next(-1,2);
newval = this.progressBar1.Value + stp;
if (newval > this.progressBar1.Maximum)
newval = this.progressBar1.Maximum;
else if (newval < this.progressBar1.Minimum)
newval = this.progressBar1.Minimum;

this.progressBar1.Value = newval;
Thread.Sleep(100);
}
}
注意:这是创建线程的基础代码。此段代码是一个无限循环,它随机增加或减小“ProgressBar1”中的值,然后等待 100 毫秒后再继续。
为“Form1”添加以下 Load 事件处理程序。此段代码将新建一个新线程,使该线程成为后台线程,然后启动该线程。private void Form1_Load(object sender, System.EventArgs e)
{
Thread trd = new Thread(new ThreadStart(this.ThreadTask));
trd.IsBackground = true;
trd.Start();
}
返回页首
确认它可以使用
生成并运行该应用程序。请注意,“ProgressBar1”中的值会随机更改。这是新线程在起作用。
要演示主线程独立于更改“ProgressBar1”值的线程,请单击窗体上的按钮。会出现一个对话框,其中显示下面的错误信息:This is the main thread
Wait for input.请注意,“ProgressBar1”中的值会继续更改。
 楼主| 发表于 2007-8-1 14:00:54 | 显示全部楼层

回复: 【转帖】c#线程

3)example 2
以前在使用VB来实现多线程的时候,发现有一定的难度。虽然也有这样那样的方法,但都不尽人意,但在C#中,要编写多线程应用程序却相当的简单。这篇文章将作简要的介绍,以起到抛砖引玉的作用!
.NET将关于多线程的功能定义在System.Threading名字空间中。因此,要使用多线程,必须先声明引用此名字空间(using System.Threading;)。
即使你没有编写多线程应用程序的经验,也可能听说过“启动线程”“杀死线程”这些词,其实除了这两个外,涉及多线程方面的还有诸如“暂停线程”“优先级”“挂起线程”“恢复线程”等等。下面将一个一个的解释。
a.启动线程
顾名思义,“启动线程”就是新建并启动一个线程的意思,如下代码可实现:
Thread thread1 = new Thread(new ThreadStart( Count));
其中的 Count 是将要被新线程执行的函数。
b.杀死线程
“杀死线程”就是将一线程斩草除根,为了不白费力气,在杀死一个线程前最好先判断它是否还活着(通过 IsAlive 属性),然后就可以调用 Abort 方法来杀死此线程。
c.暂停线程
它的意思就是让一个正在运行的线程休眠一段时间。如 thread.Sleep(1000); 就是让线程休眠1秒钟。
d.优先级
这个用不着解释了。Thread类中有一个ThreadPriority属性,它用来设置优先级,但不能保证操作系统会接受该优先级。一个线程的优先级可分为5种:Normal, AboveNormal, BelowNormal, Highest, Lowest。具体实现例子如下:
thread.Priority = ThreadPriority.Highest;
e.挂起线程
Thread类的Suspend方法用来挂起线程,知道调用Resume,此线程才可以继续执行。如果线程已经挂起,那就不会起作用。
if (thread.ThreadState = ThreadState.Running)
{
thread.Suspend();
}
f.恢复线程
用来恢复已经挂起的线程,以让它继续执行,如果线程没挂起,也不会起作用。
if (thread.ThreadState = ThreadState.Suspended)
{
thread.Resume();
}
下面将列出一个例子,以说明简单的线程处理功能。此例子来自于帮助文档。
using System;
using System.Threading;
// Simple threading scenario: Start a static method running
// on a second thread.
public class ThreadExample {
// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
public static void ThreadProc() {
for (int i = 0; i < 10; i++) {
Console.WriteLine("ThreadProc: {0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}

public static void Main() {
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate that represents the method to be executed on the
// thread. C# simplifies the creation of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
// Start ThreadProc. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
t.Start();
//Thread.Sleep(0);

for (int i = 0; i < 4; i++) {
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}

Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}

此代码产生的输出类似如下内容:
Main thread: Start a second thread.
Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
Main thread: Call Join(), to wait until ThreadProc ends.
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
Main thread: ThreadProc.Join has returned. Press Enter to end program.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2024-5-9 11:24 , Processed in 0.041853 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表