调用同一函数VS2010 _beginthread的多线程 [英] Multithreading in calling a same function VS2010 _beginthread

查看:514
本文介绍了调用同一函数VS2010 _beginthread的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在C中使用VS2010 Prof功能,它读取1个数据文件(来自1000个数据文件),做一些分析并写入一些数据文件在另一个目录中,然后继续读写到最后。通常我的函数是myfunc(int start_index,int end_index,char * read_file_directory)。为了改善运行时间我使用_beginthread创建了5个线程,每个线程调用myfunc,如下所示:

线程1:myfunc(1,200,read_directory)

线程1:myfunc(201,400, read_directory)

线程1:myfunc(401,600,read_directory)

线程1:myfunc(6011,800,read_directory)

线程1:myfunc (801,1000,read_directory)

myfunc工作得非常好,超过1000个文件,我用Visual Leak Detector 2.3检查了它,但它没有显示内存泄漏。一旦使用多线程,我的程序就会给我访问冲突。我写了一些小函数,并在myfunc中调用它们,我也在使用CLAPACK(32位发行版)和fftw3f。我完全糊涂了,是什么原因?如何解决它的任何帮助表示赞赏。

Hello all,

I have function in C using VS2010 Prof, which reads 1 data file(from 1000 data files), do some analysis and writes some data files in another directory, then continues reading and writing to the end. generally my function is myfunc(int start_index,int end_index,char *read_file_directory). To improve the runtimes I created 5 threads using _beginthread each of which calls myfunc as follows:
thread 1: myfunc(1,200,read_directory)
thread 1: myfunc(201,400,read_directory)
thread 1: myfunc(401,600,read_directory)
thread 1: myfunc(6011,800,read_directory)
thread 1: myfunc(801,1000,read_directory)
myfunc works very well over 1000 files and I checked it with Visual Leak Detector 2.3, however it shows no memory leak. As soon as using multithreading, my program gives me access violation. I wrote some small functions and call them in myfunc also I am using CLAPACK (32bit release version) and fftw3f. I am totally confused, what is the reason? how I can fix it any help is appreciated.

推荐答案

尝试从硬盘驱动器并行读取文件通常效率不高。

您可以做的是将循环中的磁盘顺序读取到内存中,然后启动一个分析数据并将结果写回文件的线程。当线程方法执行完毕后,它会退出。



您可以尝试使用 CriticalSection [ ^ ]为了避免同步磁盘操作,看看你是否在执行时间内获得或失去了或者你是否在没有CriticalSection的情况下获取访问错误。 (如果你使用不同的文件名写入磁盘,你不应该这样做。



如果这是一个好方法或不取决于文件的大小和多少分析花费的时间。

这种方法的一个风险是,如果分析时间比读取文件的时间长,你可能会遇到很多线程。
There is often not very efficient to try to read files from the hard drive in parallel.
What you can do instead is to read the the files from disk sequential in a loop into memory and then start a thread that analysis the data and writes the result back to a file. When the thread method is done executing, it exits.

You can try to use a CriticalSection[^] in order to avoid simultanious disk operation and see if you gain or lose in execution time or if you get access errors without the CriticalSection. (Which you shouldn't if you write to disk using different file names.

If this is a good approach or not depends on the size of the files and how much time the analysis takes.
One risk with this approach is that you might end up with a lot of threads if the analysis time is long compared to the time to read the file.


这篇关于调用同一函数VS2010 _beginthread的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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