N个线程同步 - 人为的例子 [英] N threads synchronization - contrived example

查看:52
本文介绍了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

推荐答案

您可以等待上次作业完成后设置的自动复位事件

或使用Thread.Join等待N个线程完成工作1并退出,然后开始

更多工作2的线程,等等。


-

William Stacey [C#MVP]


< em ************** @ fastmail.fmwrote in message

新闻:11 ********************** @ h48g2000cwc.googlegr oups.com ...

|您好

|

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

|这样的事情,他们每个人都知道如何在一个工作中完成自己的工作。

|自足的方式)。如何协调它们以便它们都等待

|直到他们都开始完成一份工作才开始他们的每一个

|接下来的工作。

|

|我一直在考虑这一天,似乎找不到一个

|解决方案。

|

| Emma

|
You could wait on an autoreset event that gets set after last job finished
or use Thread.Join to wait on N threads to finish job 1 and exit, then start
more threads for job 2, etc.

--
William Stacey [C# MVP]

<em**************@fastmail.fmwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
| 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
|


William Stacey [C#MVP]写道:
William Stacey [C# MVP] wrote:

You可以等待在最后一个工作完成后设置的自动复位事件

或使用Thread.Join等待N个线程完成工作1并退出,然后启动

更多主题for work 2,etc
You could wait on an autoreset event that gets set after last job finished
or use Thread.Join to wait on N threads to finish job 1 and exit, then start
more threads for job 2, etc



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

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

为了协调自己,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.


em ************** @ fastmail.fm 写道:

William Stacey [C#MVP]写道:
William Stacey [C# MVP] wrote:

您可以等待获得的自动复位事件在上一个工作完成后设置

或使用Thread.Join等待N个线程完成工作1并退出,然后启动

更多线程工作2等等
You could wait on an autoreset event that gets set after last job finished
or use Thread.Join to wait on N threads to finish job 1 and exit, then start
more threads for job 2, etc



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

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

为了协调自己,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


0)
0)



{

休息;

}


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个线程同步 - 人为的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆