同步两个线程类别

本文关键字:线程 两个 同步 | 更新日期: 2023-09-27 18:24:38

我正在尝试开发一个有两个线程类别的应用程序:A线程和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();
    }
}

}

同步两个线程类别

问题是什么?您可以使用锁锁定和AutoResetEvent而不是信号量。