JSB*_*ոգչ 63 c# multithreading
我正在编写我正在编写的代码中的常见模式,我需要等待组中的所有线程完成,并且超时.超时应该是所有线程完成所需的时间,因此简单地为每个线程执行thread.Join(timeout)将不起作用,因为可能的超时是超时*numThreads.
现在我做类似以下的事情:
var threadFinishEvents = new List<EventWaitHandle>();
foreach (DataObject data in dataList)
{
// Create local variables for the thread delegate
var threadFinish = new EventWaitHandle(false, EventResetMode.ManualReset);
threadFinishEvents.Add(threadFinish);
var localData = (DataObject) data.Clone();
var thread = new Thread(
delegate()
{
DoThreadStuff(localData);
threadFinish.Set();
}
);
thread.Start();
}
Mutex.WaitAll(threadFinishEvents.ToArray(), timeout);
Run Code Online (Sandbox Code Playgroud)
但是,对于这种事情,似乎应该有一个更简单的习语.
Mar*_*wis 26
我仍然认为使用Join更简单.记录预期的完成时间(如现在+超时),然后在循环中执行
if(!thread.Join(End-now))
throw new NotFinishedInTime();
Run Code Online (Sandbox Code Playgroud)
T. *_*ter 22
使用.NET 4.0,我发现System.Threading.Tasks更容易使用.这是旋转等待循环,对我来说可靠.它会阻塞主线程,直到完成所有任务.还有Task.WaitAll,但这对我来说并不总是有用.
for (int i = 0; i < N; i++)
{
tasks[i] = Task.Factory.StartNew(() =>
{
DoThreadStuff(localData);
});
}
while (tasks.Any(t => !t.IsCompleted)) { } //spin wait
Run Code Online (Sandbox Code Playgroud)
由于问题得到了解决,我将继续发布我的解决方案.
using (var finished = new CountdownEvent(1))
{
for (DataObject data in dataList)
{
finished.AddCount();
var localData = (DataObject)data.Clone();
var thread = new Thread(
delegate()
{
try
{
DoThreadStuff(localData);
threadFinish.Set();
}
finally
{
finished.Signal();
}
}
);
thread.Start();
}
finished.Signal();
finished.Wait(YOUR_TIMEOUT);
}
Run Code Online (Sandbox Code Playgroud)
在我的脑海中,你为什么不只是Thread.Join(超时)并从总超时中删除加入所花费的时间?
// pseudo-c#:
TimeSpan timeout = timeoutPerThread * threads.Count();
foreach (Thread thread in threads)
{
DateTime start = DateTime.Now;
if (!thread.Join(timeout))
throw new TimeoutException();
timeout -= (DateTime.Now - start);
}
Run Code Online (Sandbox Code Playgroud)
编辑:现在代码更少伪.不明白为什么你会修改答案-2当你修改的答案+4完全相同,只是不那么详细.
这不回答问题(没有超时),但我做了一个非常简单的扩展方法来等待集合的所有线程:
using System.Collections.Generic;
using System.Threading;
namespace Extensions
{
public static class ThreadExtension
{
public static void WaitAll(this IEnumerable<Thread> threads)
{
if(threads!=null)
{
foreach(Thread thread in threads)
{ thread.Join(); }
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后你只需致电:
List<Thread> threads=new List<Thread>();
//Add your threads to this collection
threads.WaitAll();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
101792 次 |
最近记录: |