同步两个线程类别

时间:2015-11-27 08:26:52

标签: c# multithreading

我正在尝试开发一个应用程序,其中有两个线程类别:一个线程和一个线程B.  如果线程A在临界区中,则线程B必须等到所有线程A完成。  如果线程B在临界区中,则线程A必须等到所有线程A完成

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplicationPontAVoieUnique
{
class Program
{
    public static int nbrA=0;
    public static int nbrB = 0;

    public static Semaphore semA;
    public static Semaphore semB;
    public static Semaphore semNbrA;
    public static Semaphore semNbrB;

    static void Main(string[] args)
    {
        semNbrA = new Semaphore(1, 2);
        semNbrB = new Semaphore(1, 2);

        semA = new Semaphore(0, 3);
        semB = new Semaphore(0, 3);


        Thread A1 = new Thread(new ThreadStart(ActionThreadA));
        A1.IsBackground = true;
        A1.Name = "A1";
        A1.Start();

        Thread B1 = new Thread(new ThreadStart(ActionThreadB));
        B1.IsBackground = true;
        B1.Name = "B1";
        B1.Start();

         Thread A2 = new Thread(new ThreadStart(ActionThreadA));
        A2.IsBackground = true;
        A2.Name = "A2";
        A2.Start();

        Thread B2 = new Thread(new ThreadStart(ActionThreadB));
        B2.IsBackground = true;
        B2.Name = "B2";
        B2.Start();

        Thread A3 = new Thread(new ThreadStart(ActionThreadA));
        A3.IsBackground = true;
        A3.Name = "A3";
        A3.Start();

        Thread B3 = new Thread(new ThreadStart(ActionThreadB));
        B3.IsBackground = true;
        B3.Name = "B3";
        B3.Start();

        Thread A4 = new Thread(new ThreadStart(ActionThreadA));
        A4.IsBackground = true;
        A4.Name = "A4";
        A4.Start();

        Thread B4 = new Thread(new ThreadStart(ActionThreadB));
        B4.IsBackground = true;
        B4.Name = "B4";
        B4.Start();

        Console.ReadLine();
    }

    static void ActionThreadA()
    {
        semNbrA.WaitOne();

        nbrA++;
        if (nbrA == 1)
        {
            Console.WriteLine(Thread.CurrentThread.Name + " I'am the first A ");
            semB.WaitOne();
        }
        semNbrA.Release();

        Console.WriteLine(Thread.CurrentThread.Name + " I'am in the cs ");

        Thread.Sleep(5000); //sc

        semNbrA.WaitOne();

        nbrA--;
        if (nbrA == 0)
        {
            Console.WriteLine(Thread.CurrentThread.Name + " I'am the last A ");
            semB.Release();
        }
        semNbrA.Release();


    }
    static void ActionThreadB()
    {
        semNbrB.WaitOne();

        nbrB++;
        if (nbrB == 1)
        {
            Console.WriteLine(Thread.CurrentThread.Name + " I'am the first B ");
            semA.WaitOne();
        }
        semNbrB.Release();

        Console.WriteLine(Thread.CurrentThread.Name + " I'am in the cs ");
        Thread.Sleep(5000);

        semNbrB.WaitOne();

        nbrB--;
        if (nbrB == 0)
        {
            Console.WriteLine(Thread.CurrentThread.Name + " I'am the last B ");
            semA.Release();
        }
        semNbrB.Release();
    }

}

}

1 个答案:

答案 0 :(得分:0)

问题是什么? 您可以使用锁定lockAutoResetEvent代替信号量。