本文介绍了N个线程同步 - 人为的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




假设您有N个线程做某些工作(不是来自共享队列或

这样的事情,他们每个人都知道怎么做

自包含方式的一系列工作)。如何协调他们以便他们都等待
,直到他们完成一份工作,然后开始他们的每一个下一个工作。


我一直在考虑这一天,似乎无法找到一个

的解决方案。


Emma

Hi

Say you had N threads doing some jobs (not from a shared queue or
anything like that, they each know how to do their own set of jobs in a
self-contained way). How can you coordinate them so that they all wait
until they''ve all done one job before starting off on each of their
next jobs.

I have been thinking about this for a day and can''t seem to find a
solution.

Emma

推荐答案




这给了我一些好主意!我当时认为线程

不应该直接了解彼此,而且我的抽象问题也包括没有任何协调它们的东西。在

为了协调自己,AutoResetEvent的想法是我最初尝试的
,但是无法让它工作。你觉得信号量是什么?我应该用什么?b $ b?我发现它有点难以让它工作!


艾玛。

That''s given me some good ideas! I was thinking that the threads
shouldn''t know about each other directly, and also my abstract problem
includes that there is nothing as such that is coordinating them. In
order to coordinate themselves, the AutoResetEvent idea is what I tried
initially but couldn''t get it to work. Do you think a Semaphore is what
I should use? I''m finding it a bit hard to get it working though!

Emma.




这给了我一些好主意!我当时认为线程

不应该直接了解彼此,而且我的抽象问题也包括没有任何协调它们的东西。在

为了协调自己,AutoResetEvent的想法是我最初尝试的
,但是无法让它工作。你觉得信号量是什么?我应该用什么?b $ b?我发现它有点难以让它工作!


That''s given me some good ideas! I was thinking that the threads
shouldn''t know about each other directly, and also my abstract problem
includes that there is nothing as such that is coordinating them. In
order to coordinate themselves, the AutoResetEvent idea is what I tried
initially but couldn''t get it to work. Do you think a Semaphore is what
I should use? I''m finding it a bit hard to get it working though!






所以,我有一个快速的时刻来敲一些有用的东西......


它使用N个AutoResetEvents并且每个线程等待它自己的事件,然后

以循环方式表示下一个线程。


这是我能做的最好的(即我必须使用N个同步对象)?

有人可以确认,如果我想让线程处理这个工作吗?
特定的顺序和一次一个,没有更好的方法吗?


这里有一个相关的问题:如果我只想要线程全部完成一个

任何其他线程之前的工作可以继续下一个(即转出

下一个int!)但它不必在任何特定的线程中

顺序,我可以只使用1个同步对象吗?我正试图让

Semaphore在这种情况下工作,因为它看起来确实需要



如果有人对此感兴趣,我会在工作程序中附上

以防万一:


static void Main()

{

int threadCount;


while(true)

{

Console.Write("输入线程数:");

string input = Console.ReadLine();


if(int .TryParse(输入,输出threadCount)&& threadCount

Hi

So, I got a quick moment to knock up something that works ...

It uses N AutoResetEvents and each thread waits on its own event, then
signals the next one when finished, in circular fashion.

Is this the best I can do (i.e. I must use N synchronization objects)?
Can someone confirm that, if I want the threads to process jobs in this
particular order and one at a time, there is no better way?

Here''s a related question: if I just want threads to all complete one
job before any other thread can go on to the next one (i.e. dump out
the next int!) but it doesn''t have to be in any particular thread
order, can I use just 1 synchronization object? I''m trying to get the
Semaphore to work in this situation because it seems exactly what''s
required!

If anyone is interested, fancies adapting and improving, I''ve enclosed
below a working program just in case:

static void Main()
{
int threadCount;

while (true)
{
Console.Write("Enter number of threads: ");
string input = Console.ReadLine();

if (int.TryParse(input, out threadCount) && threadCount



{

休息;

}


Console.WriteLine("输入正整数。);

}


AutoResetEvent [] events = new AutoResetEvent [threadCount];

ThreadConsole [] threads = new ThreadConsole [threadCount];


for(int i = 0; i< threadCount; i ++)
{

events [i] = new AutoResetEvent(false);

}


for( int i = 0;我< THREADCOUNT; i ++)

{

threads [i] =新的ThreadConsole(&Thread+

i.ToString(),events [i ],events [i + 1 == threadCount?0:i + 1]);

threads [i] .Start();

}


events [0] .Set();


Console.ReadLine();

}

}


类ThreadConsole

{

线程主题;

字符串名称;

AutoResetEvent canStart;

AutoResetEvent nextCanStart;


public ThreadConsole(字符串名称,AutoResetEvent canStart,

AutoResetEvent nextcanStart )

{

thread = new Thread(ThreadProc);

this.name = name;

this。 canStart = canStart;

this.nextCanStart = nextcanStart;

}


public void Start()

{

thread.Start();

}


void ThreadProc()

{

for(int i = 0; i< 100; i ++)

{

canStart.Wait一个();

Console.WriteLine(name +":" + i.ToString());

nextCanStart.Set();

}

}

}

干杯,

艾玛。

{
break;
}

Console.WriteLine("Enter a positive integer.");
}

AutoResetEvent[] events = new AutoResetEvent[threadCount];
ThreadConsole[] threads = new ThreadConsole[threadCount];

for (int i = 0; i < threadCount; i++)
{
events[i] = new AutoResetEvent(false);
}

for (int i = 0; i < threadCount; i++)
{
threads[i] = new ThreadConsole("Thread " +
i.ToString(), events[i], events[i + 1 == threadCount ? 0 : i + 1]);
threads[i].Start();
}

events[0].Set();

Console.ReadLine();
}
}

class ThreadConsole
{
Thread thread;
string name;
AutoResetEvent canStart;
AutoResetEvent nextCanStart;

public ThreadConsole(string name, AutoResetEvent canStart,
AutoResetEvent nextcanStart)
{
thread = new Thread(ThreadProc);
this.name = name;
this.canStart = canStart;
this.nextCanStart = nextcanStart;
}

public void Start()
{
thread.Start();
}

void ThreadProc()
{
for (int i = 0; i < 100; i++)
{
canStart.WaitOne();
Console.WriteLine(name + ": " + i.ToString());
nextCanStart.Set();
}
}
}
Cheers,

Emma.


这篇关于N个线程同步 - 人为的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 18:44