多线程文件读取为每个线程生成相同的结果
Multi-threaded file reading produces the same result for each thread
基本上,我遇到的问题出在标题中,我正在尝试创建一个多线程应用程序来读取和汇总文件的内容,这在一个线程上可以正常工作。但是,当引入更多时,它们会产生相同的输出。我该如何解决这个问题?
代码
void *sumThread(void *);
pthread_mutex_t keepOut = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t keepOutSum = PTHREAD_MUTEX_INITIALIZER;
int counter = 0, line_count = 0;
char* loc;
double total = 0;
void split(const string& s, char c, vector<string>& v)
{
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos)
{
v.push_back(s.substr(i, j - i));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length()));
}
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
cerr << "Usage: " << argv[0] << " filename" << endl;
return 1;
}
string line;
loc = argv[1];
ifstream myfile(argv[1]);
myfile.unsetf(ios_base::skipws);
line_count = std::count(std::istream_iterator<char>(myfile),
std::istream_iterator<char>(),
'n');
myfile.clear();
myfile.seekg(-1, ios::end);
char lastChar;
myfile.get(lastChar);
if (lastChar != 'r' && lastChar != 'n')
line_count++;
myfile.setf(ios_base::skipws);
myfile.clear();
myfile.seekg(0, ios::beg);
pthread_t thread_id[NTHREADS];
for (int i = 0; i < NTHREADS; ++i)
{
pthread_create(&thread_id[i], NULL, sumThread, NULL);
}
for (int i = 0; i < NTHREADS; ++i)
{
pthread_join(thread_id[i], NULL);
}
cout << setprecision(2) << fixed << total << endl;
return 0;
}
void *sumThread(void *)
{
pthread_mutex_lock(&keepOut);
int threadNo = counter;
counter++;
pthread_mutex_unlock(&keepOut);
ifstream myfile(loc);
double runningTotal = 0;
string line;
if (myfile.is_open())
{
for (int i = threadNo; i < line_count; i += NTHREADS)
{
vector < string > parts;
getline(myfile, line);
// ... and process out the 4th element in the CSV.
split(line, ',', parts);
if (parts.size() != 3)
{
cerr << "Unable to process line " << i
<< ", line is malformed. " << parts.size()
<< " parts found." << endl;
continue;
}
// Add this value to the account running total.
runningTotal += atof(parts[2].c_str());
}
myfile.close();
}
else
{
cerr << "Unable to open file";
}
pthread_mutex_lock(&keepOutSum);
cout << threadNo << ": " << runningTotal << endl;
total += runningTotal;
pthread_mutex_unlock(&keepOutSum);
pthread_exit (NULL);
}
示例输出
2: -46772.4
0: -46772.4
1: -46772.4
3: -46772.4
-187089.72
每个线程都应该读取和汇总文件中的数字,然后在完成后将它们相加。但是,线程似乎都返回相同的数字,即使 threadNo 变量明显不同,如输出所示。
你的问题在这里:
for (int i = threadNo; i < line_count; i += NTHREADS) {
vector<string> parts;
getline(myfile, line);
getline(( 不知道 i
的值,所以它仍然从文件中读取相邻的行,而不跳过任何行。 因此,所有线程都在读取文件的相同前几行。
相关文章:
- QtConcurrent - 在发布到 UI 线程的数千个结果中保持 GUI 响应
- C++ 多线程给出不同的结果
- C++ 线程端回调并返回结果
- 在向量上声明缩减,在 1 个线程上运行给出的结果与没有 openmp 的结果不同
- 错误 C2064:term 的计算结果不是采用 1 个参数的函数 - 关于线程的一些东西
- 提升线程:术语的计算结果不为 0 个参数
- 如何在C++中获取线程的结果
- 多线程合并选项的奇怪结果
- 多线程文件读取为每个线程生成相同的结果
- 在多线程工作负载上解释Gperftools的结果
- 从多个线程收集结果的缓存友好方式
- 将线程的结果转储到向量中是线程安全的吗
- 从 IOCP 线程调用 WSAGetLastError() 将返回不正确的结果
- 与单线程相比,c++/java的多线程性能结果参差不齐
- 如何从线程的结果返回Qt对象(Qtfutur)
- CUDA 线程在全局内存中的私有位置写入时出现错误结果
- 我的线程工作不好,它给出了所有结果,最后没有一个接一个,GUI 在线程运行期间挂起?
- 从非线程阻塞消息框获取结果
- 线程合并排序给出无效结果
- C++ 将结果从多个线程返回到数组中