两个线程的同步

时间:2009-05-12 13:23:52

标签: c# multithreading synchronization

我在c#中有两个线程。现在我需要等待一个特定的语句执行才能继续执行另一个线程,这显然是同步的情况。 是否有任何代码可以像使用内置方法一样进行此操作?

这是代码示例:

    public void StartAccept()
    {
            try
            {
                newSock.BeginAccept(new AsyncCallback(Accepted), newSock);
            }
            catch (ArgumentException)
            {
                MessageBox.Show("Error in arguments while using begin-accept", "Error", MessageBoxButtons.OK);
            }
            catch (ObjectDisposedException)
            {
                MessageBox.Show("socket closed while using begin-accept", "Error", MessageBoxButtons.OK);
            }
            catch (SocketException)
            {
                MessageBox.Show("Error accessing socket while using begin-accept", "Error", MessageBoxButtons.OK);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show("Invalid operation while using begin-accept", "Error", MessageBoxButtons.OK);
            }
            catch (Exception)
            {
                MessageBox.Show("Exception occurred while using begin-accept", "Error", MessageBoxButtons.OK);
            }

    }

它接收来自所需主机的数据,该数据由代码选择:

    private void listBox1_Click(object sender, EventArgs e)
    {



        String data = (String)this.listBox1.SelectedItem;

        ip = Dns.GetHostAddresses(data);

        clientIP = new IPEndPoint(ip[0], 5555);

        newSock.Bind(clientIP);
        newSock.Listen(100);

    }

因此,为了开始接收数据,我需要将套接字初始化为特定的远程主机,这是在我单击列表框中显示的某个主机时完成的。 为此,我需要同步。

3 个答案:

答案 0 :(得分:11)

查看AutoResetEventManualResetEvent。它们是使线程之间的同步成为可能的信号。

需要等待某事完成的第一个线程将执行myEvent。WaitOne(),它会阻塞,直到另一个线程调用myEvent。Set()。

假设我们有两个线程,其中一个线程需要在另一个线程可以继续之前进行某种初始化。然后在两者之间共享一个AutoResetEvent,我们称之为myEvent。

// Signal example

using System;
using System.Threading;

class MySync
{
    private readonly AutoResetEvent _myEvent;
    public MySync(AutoResetEvent myEvent)
    {
        _myEvent = myEvent;
    }

    public void ThreadMain(object state)
    {
        Console.WriteLine("Starting thread MySync");
        _myEvent.WaitOne();
        Console.WriteLine("Finishing thread MySync");
    }
}

class Program
{
    static void Main(string[] args)
    {
        AutoResetEvent myEvent = new AutoResetEvent(false);
        MySync mySync = new MySync(myEvent);
        ThreadPool.QueueUserWorkItem(mySync.ThreadMain);
        Console.WriteLine("Press enter to continue...");
        Console.ReadLine();
        myEvent.Set();
        Console.WriteLine("Press enter to continue...");
        Console.ReadLine();
        Console.WriteLine("Finishing");
    }
}

请勿将此与访问顺序无关紧要的共享资源混淆。例如,如果您有共享列表或共享字典,则需要将其包装在互斥锁中,以保证它们正确执行。

// Mutex example

object mySync = new object();
Dictionary<int, int> myDict = new Dictionary<int, int>();

void threadMainA()
{
    lock(mySync)
    {
        mySync[foo] = bar;
    }
}

void threadMainB()
{
    lock(mySync)
    {
        mySync[bar] = foo;
    }
}

答案 1 :(得分:3)

您可以使用AutoResetEvent。

在下面的示例中,两个方法由不同的线程调用,DoSomethingA()将在DoSomethingB()启动之前执行并完成:

AutoResetEvent resetEvent = new AutoResetEvent(false);

void ThreadWorkerA()
{
    // perform some work
    DoSomethingA();

    // signal the other thread
    resetEvent.Set();
}

void ThreadWorkerB()
{
    // wait for the signal
    resetEvent.WaitOne();

    // perform the new work
    DoSomethingB();
}

注意:请记住处置AutoResetEvent:)

答案 2 :(得分:-2)

Java有一个名为Join的东西。我怀疑在C#中也会有一个预定义的方法。

相关问题