最近一直在学习C#的多线程编程,发现多线程编程并不容易。在启动访问相同数据的多个线程时,会间歇性地遇到难以发现的问题。下面来讨论与线程相关的问题:争用条件。如果两个或多个线程访问相同的对象,或者访问不同步的共享状态,就会出现争用条件。看下面的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
   8:  
namespace ThreadingIssues
  10: {
class StateObject
  12:     {
int state = 5;
object();
/// <summary>
/// 当state等于5时,改变state的值
/// </summary>
int loop)
  20:         {
if (state == 5)
  22:                 {

23: state++;

//递增之后立即验证state现在是否6

);
  25:                 }
  26:             state = 5;
  27:         }
  28:     }
  29:  
//创建一个分配给任务的一个方法
class SampleTask
  32:     {   
object o)
  34:         {
);
as StateObject;
  37:  
int i = 0;
true)
  40:             {
  41:                  state.ChangeState(i++);
  42:             }
  43:         }
  44:     }
  45:  
class Program
  47:    {
string[] args)
  49:        {
new StateObject();
int i = 0; i < 20; i++)
  52:            {
new SampleTask().RaceCondition, state).Start();
  54:            }
  55:            Thread.Sleep(10000);
  56:        }
  57:    }
  58: }
  59:  
  60:  
  61:  

运行这个示例的结果:

使用锁解决线程争用的问题

 

在运行到63414个循环时,出现争用条件的程序断言。多次启动应用程序,总会得到不同的结果。要避免这个问题,可以锁定共享的对象。这个过程通过在使用共享对象的方法中进行锁定以及将共享对象设置为线程安全的对象来实现。

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
   8:  
namespace ThreadingIssues
  10: {
class StateObject
  12:     {
int state = 5;
object();
/// <summary>
/// 当state等于5时,改变state的值
/// </summary>
int loop)
  20:         {
lock (sync)
  22:             {
if (state == 5)
  24:                 {
  25:                     state++;
);
  27:                 }
  28:             }
  29:             state = 5;
  30:         }
  31:     }
  32:  
//创建一个分配给任务的一个方法
class SampleTask
  35:     {   
object o)
  37:         {
);
as StateObject;
  40:  
int i = 0;
true)
  43:             {
lock(state)
  45:                 {
  46:                  state.ChangeState(i++);
  47:                 }
  48:             }
  49:         }
  50:     }
  51:  
class Program
  53:     {
string[] args)
  55:         {
new StateObject();
int i = 0; i < 20; i++)
  58:             {
new SampleTask().RaceCondition, state).Start();
  60:             }
  61:             Thread.Sleep(10000);
  62:         }
  63:     }
  64: }

运行结果如下:

使用锁解决线程争用的问题

总结:使用lock语句锁定共享state对象的代码块,只有一个线程能在锁定块中处理共享的state对象。由于这个对象在所有的线程之前共享,因此如果一个线程锁定了state对象,另外一个线程必须等待该锁定的解除。一旦接收锁定,该线程就拥有该锁定,直到该锁定块的末尾才解除锁定。如果改变state变量引用的对象的每个线程都使用一个锁定,就不会出现争用条件。

在将共享对象state设置为线程安全的对象时,由于StateObject类内部的变量state(注意这个state变量和前面的state对象不是同一个)是int类型,由于lock不能锁定值类型本身(只有引用类型才能用于锁定),因此需要定义一个object类型的变量sync,将它用于lock语句。如果每次state的值更改时,都使用同步对象来锁定,就不会出现争用条件.

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
  • 2019-12-16
  • 2019-07-27
  • 2022-12-23
猜你喜欢
  • 2021-04-13
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2021-08-06
  • 2022-12-23
  • 2021-07-14
相关资源
相似解决方案