我有一个线程池,所有线程都有一组固定的图像。假设他们每100个图像。我需要检测哪个图像包含驻留在图像库中的另一个图像。
主题1 - 100张图片
主题2 - 100张图片
主题3 - 100张图片
主题4 - 100张图片
图像库 - 50张图片
现在我需要所有线程查看Image基础内部以查看它们所持有的图像之一是否类似于图像库之一。我完成了图像匹配,我担心的是多个线程是否可能打开相同的图像文件。解决这个问题的正确方法是什么?不要“锁定”每个IO的所有其他线程。
谢谢!
答案 0 :(得分:0)
这样的事情怎么样,每个线程都有一个对图像库的引用,并提供一个代表,将为图像库中的每个文件调用?这是图像库可能的样子的骨架。
public class ImageBank {
public delegate bool ImageProcessorDelegate(String imageName);
private readonly List<String> _imageBank;
public ImageBank()
{
// initialize _imageBank with list of image file names
}
private int ImageBankCount {
get {
lock(_imageBank) {
return _imageBank.Count;
}
}
}
private List<String> FilterImageBank(ISet<String> exclude)
{
lock(_imageBank)
{
return _imageBank.Where(name => !exclude.Contains(name)).ToList();
}
}
public void ProcessImages(ImageProcessorDelegate imageProcessor)
{
var continueProcessing = true;
var processedImages = new HashSet<String>();
var remainingImages = new List<String>();
do
{
remainingImages = FilterImageBank(processedImages);
while(continueProcessing && remainingImages.Any())
{
var currentImageName = remainingImages[0];
remainingImages.RemoveAt(0);
// protect this image being accessed by multiple threads.
var mutex = new Mutex(false, currentImageName);
if (mutex.WaitOne(0))
{
try
{
// break out of loop of the processor found what it was looking for.
continueProcessing = imageProcessor(currentImageName);
}
catch (Exception)
{
// exception thrown by the imageProcessor... do something about it.
}
finally
{
// add the current name to the set of images we've alread seen and reset the mutex we acquired
processedImages.Add(currentImageName);
mutex.ReleaseMutex();
}
}
}
}
while(continueProcessing);
}
}
然后,每个线程都会有一个图像列表(_myImageList
)和一个ThreadProc
看起来像这样:
void ThreadProc(object bank)
{
var imageBank = bank as ImageBank;
foreach(var myImage in _myImageList)
{
imageBank.ProcessImages(imageName =>
{
// do something with imageName and myImage
// return true to continue with the next image from the image bank
// or false to stop processing more images from the image bank
}
);
}
}
答案 1 :(得分:-1)
假设所有线程都有相同的图像集,并且假设这些文件的pahts在列表或其他集合中,您可以尝试这样的事情:
// A shared collection
List<string> paths = new List<string>();
// Fill this collection with your fixed set.
IEnumerator<T> e = paths.GetEnumerator();
// Now create all threads and use e as the parameter. Now all threads have the same enumerator.
// Inside each thread you can do this:
while(true)
{
string path;
lock(e)
{
if (!e.MoveNext())
return; // Exit the thread.
path = e.Current;
}
// From here, open the file, read the image, process it, etc.
}
在此示例中,您只对锁定器进行锁定。只有一个线程可以同时从中读取。因此,每次调用它时,都会产生不同的路径。
在锁外,您可以进行所有处理,I / O等。
当然,集合也可以是另一种类型,如数组。