Windows线程C ++ - 访问冲突

时间:2012-02-17 17:10:11

标签: c++ windows multithreading

我是Windows新线程的新手,非常感谢任何建议。我创建了一个小程序来演示我得到的访问冲突。 这是test.h:

#ifndef TEST_H
#define TEST_H

using namespace std;

#include <windows.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string> 
#include <vector>

/**************************************************************************************************/
template<typename T>
string toString(const T&x){

        stringstream output;
        output << x;
        return output.str();    
}

/**************************************************************************************************/
//custom data structure for threads to use.
// This is passed by void pointer so it can be any data type
// that can be passed using a single void pointer (LPVOID).
struct tempData {
    int threadID;
    vector<string> filenames;

    tempData(){}
    tempData(vector<string> f, int tid) {
        filenames = f;
        threadID = tid;
    }
 };

 /**************************************************************************************************/
 static DWORD WINAPI tempThreadFunction(LPVOID lpParam){ 
    tempData* pDataArray;
    pDataArray = (tempData*)lpParam;

            string fileName = pDataArray->filenames[pDataArray->threadID];
            ifstream fileHandle(fileName.c_str());
            string output = toString(pDataArray->threadID);
            ofstream out(output.c_str());

            string name;
            int currentNum, num;
            vector<string> nameVector;
            vector<float> data;
            float currentData;
            int index = 0;

            fileHandle >> num;

            while(!fileHandle.eof()){

                fileHandle >> name >> currentNum; 
                nameVector.push_back(name);

                for(int i=0;i<num;i++){
                    fileHandle >> currentData;
                    data.push_back(currentData);
                }

                //grab extra white space
                char d;
                while(isspace(d=fileHandle.get()))      { ;}
                if(!fileHandle.eof()) { fileHandle.putback(d); }

                index++;

                cout << "Thread " << pDataArray->threadID << '\t' << index << endl;
                out << name << '\t' << "Thread " << pDataArray->threadID << '\t' << index << endl;
            }

            fileHandle.close();
            out.close();

            cout << "Thread " << pDataArray->threadID << " read " << nameVector.size() << " lines." << endl;
    }
#endif

这是test.cpp

#include "test.h"

/**************************************************************************************************/
int main(int argc, char *argv[]){

    string fileName1 = argv[1];
    string fileName2 = argv[2];

    vector<string> fileNames; fileNames.push_back(fileName1);  fileNames.push_back(fileName2); 

    vector<tempData*> pDataArray; 
    DWORD   dwThreadIdArray[2];
    HANDLE  hThreadArray[2]; 

    //Create processor worker threads.
    for( int i=0; i<2; i++ ){
    // Allocate memory for thread data. 
        tempData* tempThread = new tempData(fileNames, i);
        pDataArray.push_back(tempThread);

        hThreadArray[i] = CreateThread(NULL, 0, tempThreadFunction, pDataArray[i], 0, &dwThreadIdArray[i]);   
    }

        //Wait until all threads have terminated.
    WaitForMultipleObjects(2, hThreadArray, TRUE, INFINITE);

    //Close all thread handles and free memory allocations.
    for(int i=0; i < pDataArray.size(); i++){
        CloseHandle(hThreadArray[i]);
        delete pDataArray[i];
    }

    return 0;
}
 /**************************************************************************************************/

线程正在读取的文件如下:

 450
 F5MMO9001C96XU 450 1.03 0.02 1.00 0.03 0.05 1.02 0.03 1.04 0.05 0.04 2.06 1.05 2.01 0.05 0.98 0.03 0.08 1.05 1.01 0.02 0.05 1.03 0.04 0.04 2.05 1.07 2.04 1.01 0.06 0.05 0.96 2.02 0.06 0.04 0.99 0.06 1.00 0.03 0.06 1.04 0.08 0.01 1.07 0.06 1.02 0.03 0.05 2.00 0.07 0.04 1.00 0.11 0.06 1.01 1.02 1.02 1.03 1.06 0.04 1.04 1.94 1.02 0.06 1.00 0.12 0.06 2.01 1.96 0.94 0.08 0.10 0.96 0.12 0.05 1.01 0.05 2.04 1.11 0.08 0.04 2.00 0.06 1.02 0.04 1.99 0.05 1.03 0.09 0.14 0.98 0.10 1.99 1.02 1.06 2.11 1.00 0.96 0.10 1.00 0.08 0.11 1.08 0.07 0.06 1.03 0.10 0.04 1.01 0.12 1.11 0.09 0.99 0.98 0.12 3.06 0.15 0.12 1.03 0.17 2.00 1.01 0.98 0.06 0.16 2.00 1.00 0.08 1.06 0.19 0.13 2.10 0.13 0.08 1.00 0.19 0.99 0.16 2.00 2.19 0.12 3.96 0.17 0.99 0.05 2.06 0.06 3.03 0.08 1.02 0.06 0.11 1.02 0.17 1.01 1.06 0.15 0.08 3.92 0.14 1.01 0.13 0.12 1.05 2.04 3.04 1.02 0.98 0.08 0.10 2.02 3.19 1.00 0.11 1.98 0.14 1.94 0.14 0.07 2.04 0.08 2.05 0.06 0.98 0.08 1.99 0.04 2.93 1.07 0.11 0.05 1.04 0.17 0.09 0.97 1.05 0.99 0.08 0.11 1.02 1.98 0.07 0.06 1.05 0.06 0.09 1.03 0.17 0.11 1.05 0.14 0.09 2.09 0.19 0.06 1.02 0.13 1.03 0.06 0.15 2.07 0.19 0.98 0.08 0.06 1.06 0.16 1.09 0.14 0.16 1.00 0.17 2.07 0.13 0.13 1.01 0.08 2.04 0.05 0.18 1.03 0.05 0.02 0.99 1.01 0.09 0.07 2.98 0.07 0.13 1.01 0.04 0.10 1.99 0.15 0.15 1.05 1.01 0.01 2.09 0.16 0.13 4.02 0.19 0.06 2.03 0.10 3.97 0.08 0.09 1.01 1.01 0.08 1.03 0.16 0.09 1.03 0.12 0.05 1.02 0.07 1.04 0.04 0.15 1.01 0.13 0.04 1.91 0.10 1.06 0.08 2.99 1.01 1.01 1.00 0.04 1.93 0.13 0.90 0.16 1.01 0.98 0.04 1.14 0.16 1.06 0.05 0.13 3.00 0.12 0.05 2.10 0.99 0.99 0.03 0.09 1.00 1.01 0.04 0.99 0.04 1.02 0.08 1.02 0.14 0.11 0.98 0.20 1.15 1.06 0.06 3.08 0.08 0.09 0.97 0.00 0.97 1.04 0.15 0.12 0.89 0.94 0.05 0.12 2.04 0.14 0.04 1.15 0.11 1.06 0.04 0.08 2.10 1.05 0.03 1.01 0.98 1.04 0.03 2.00 0.03 1.01 0.03 0.91 0.10 1.04 0.08 1.04 0.14 0.03 0.98 0.15 1.13 0.12 0.92 2.14 0.09 0.11 0.96 0.07 1.04 0.13 0.03 1.02 0.05 1.12 1.06 1.00 0.13 0.04 0.88 0.01 1.10 0.14 0.88 0.14 0.10 1.10 0.00 1.14 1.01 1.02 0.06 0.95 1.86 0.07 0.04 1.01 0.04 1.93 0.04 0.08 2.05 1.10 0.10 0.11 0.91 0.11 1.00 0.08 1.09 0.07 0.10 2.14 0.10 3.19 1.07 2.10 0.11 1.02 0.13 0.93 0.09 0.13 0.90 2.17 0.09 0.19 2.09 1.10 0.09 1.13 0.91 2.03 0.08 1.01 2.09 0.19 0.07 1.03 0.10 
 F5MMO9001DCOF4 450 0.98 0.02 1.03 0.02 0.04 1.04 0.02 1.02 0.03 0.05 2.15 1.04 2.01 0.00 0.93 0.07 0.06 1.01 0.99 0.03 0.05 1.02 0.05 0.02 2.06 1.10 2.02 0.98 0.09 0.06 1.05 2.03 0.08 0.05 1.01 0.10 1.03 0.03 0.09 1.00 0.07 0.01 1.02 0.07 0.98 0.03 0.05 1.98 0.10 0.01 1.02 0.10 0.05 1.03 1.09 1.02 1.02 1.04 0.06 0.99 1.98 0.98 0.07 1.00 0.12 0.04 2.09 1.03 1.00 0.00 0.17 2.02 0.11 0.03 0.96 0.13 2.02 0.04 2.11 0.05 1.03 0.00 1.11 1.07 2.92 1.02 1.02 0.08 0.93 1.03 2.02 0.99 1.01 0.08 1.05 0.09 0.13 1.00 0.11 0.01 2.00 0.11 0.06 1.03 0.18 0.05 1.04 0.07 0.05 1.99 0.11 0.01 0.99 0.16 0.05 1.04 0.11 0.05 1.04 0.13 0.07 1.02 0.11 0.06 2.17 0.10 0.03 1.04 2.07 0.03 0.99 0.13 0.09 0.99 1.02 0.00 0.04 0.94 1.04 0.01 0.06 1.05 1.01 0.02 1.10 0.11 0.11 1.01 0.12 0.03 1.03 0.11 0.09 1.01 1.03 1.06 2.02 0.09 0.99 1.06 1.03 0.03 1.03 0.12 0.17 0.88 0.16 0.02 1.11 2.86 1.07 0.03 0.15 2.10 1.01 0.02 0.04 0.91 0.15 0.99 0.03 1.01 0.06 1.07 0.09 0.16 1.05 0.13 3.03 1.00 1.07 0.05 0.16 0.99 0.13 0.98 0.08 0.90 2.01 1.05 0.08 2.74 0.20 0.16 1.01 0.20 2.07 0.04 2.05 0.11 1.08 0.03 0.16 1.05 0.10 0.02 0.97 0.08 0.99 0.04 0.19 1.02 1.03 0.03 1.08 0.10 1.04 0.05 0.16 1.06 1.01 0.99 0.06 0.15 1.02 1.92 0.13 0.06 1.02 1.02 2.06 0.04 0.09 1.09 0.15 0.01 0.98 0.08 1.06 0.01 2.06 1.02 1.01 0.04 1.08 0.12 0.09 0.90 0.11 0.99 0.17 1.03 1.14 0.08 2.84 0.04 0.86 0.94 1.37 0.08 2.05 0.19 0.16 0.94 0.35 0.11 2.00 0.20 0.18 0.93 0.41 0.15 0.96 2.03 0.16 1.75 0.19 1.45 0.14 1.27 0.04 0.17 2.11 0.23 3.92 0.13 0.32 1.02 2.03 0.07 1.05 0.27 0.30 1.06 0.29 0.08 0.99 0.24 1.04 0.02 0.31 1.03 0.24 0.05 1.93 0.21 0.98 0.09 3.70 1.02 1.44 1.03 0.84 2.42 0.24 1.23 0.09 1.49 2.89 0.24 0.21 3.26 0.93 0.10 2.19 1.98 1.00 0.03 0.45 1.27 1.30 0.02 0.83 0.26 1.17 0.05 1.19 0.12 0.23 0.85 0.20 1.00 0.98 0.15 2.58 0.21 0.27 1.72 0.90 0.16 0.88 0.38 0.01 1.08 1.20 0.12 0.16 2.01 0.24 0.03 1.88 1.39 1.83 0.06 1.36 0.21 0.39 0.87 0.19 0.12 0.84 0.19 1.69 0.09 1.13 0.09 1.42 0.09 1.24 0.09 1.11 0.09 0.21 0.81 0.20 0.93 0.16 1.06 1.70 2.08 0.15 0.16 1.42 0.43 1.06 0.86 1.20 0.12 1.22 0.20 0.25 0.98 0.23 0.82 0.19 0.25 1.01 0.18 1.05 0.11 0.26 0.95 0.22 0.11 1.08 0.19 1.05 1.03 0.21 0.08 2.14 0.21 1.84 0.07 0.40 1.79 1.35 0.90 0.17 1.35 1.12 0.15 1.84 1.23 2.19 0.86 1.35 0.26 0.34 1.00 
 F5MMO9001CUZ4G 450 1.04 0.01 1.02 0.03 0.04 1.00 0.02 1.01 0.04 0.08 2.06 1.02 1.97 0.03 0.99 0.05 0.07 1.07 1.03 0.02 0.06 1.03 0.05 0.02 1.99 1.04 2.06 0.99 0.09 0.05 1.01 1.98 0.08 0.06 1.00 0.06 1.03 0.05 0.05 1.02 0.11 0.04 1.03 0.06 1.04 0.03 0.06 2.04 0.09 0.05 0.98 0.08 0.06 1.03 1.02 1.03 0.98 1.05 0.07 1.01 1.95 1.05 0.05 1.00 0.11 0.05 2.03 1.96 1.02 0.01 0.11 1.03 0.12 0.02 0.98 0.07 1.97 0.03 1.02 0.04 3.03 1.01 3.02 0.05 0.17 1.01 0.19 0.06 2.00 1.05 2.07 1.03 1.01 0.10 1.04 0.09 0.12 1.03 1.04 0.04 1.01 0.12 1.03 0.05 0.09 1.02 1.00 1.01 0.09 0.12 1.06 0.12 2.01 0.01 0.99 1.05 1.03 0.06 1.05 0.10 0.12 1.02 1.03 0.06 0.05 1.00 0.11 2.00 0.07 0.14 0.98 1.05 0.07 3.04 0.13 1.05 0.12 0.07 1.03 2.03 3.07 1.02 0.99 0.16 0.05 1.98 3.08 0.96 0.08 1.97 0.10 1.96 0.08 0.10 1.98 1.03 1.04 0.07 1.03 0.13 0.16 1.03 0.20 0.07 1.01 0.14 3.08 0.97 0.14 0.05 1.09 0.15 0.06 1.02 1.00 1.01 0.06 0.12 1.02 1.99 0.11 0.03 1.01 0.98 2.02 0.02 0.18 1.06 0.14 0.02 1.03 0.15 1.00 0.03 0.15 1.02 0.15 0.04 1.04 0.13 0.09 0.99 0.16 0.06 1.03 0.15 1.05 0.10 0.16 1.01 0.18 1.99 0.14 0.09 1.05 0.09 1.99 0.04 2.05 1.03 0.10 0.05 3.14 0.15 0.14 1.01 0.11 0.07 2.01 0.12 0.09 0.96 1.00 0.03 0.09 1.02 0.19 0.08 1.03 0.15 0.12 2.14 0.18 0.05 1.02 1.06 0.18 0.04 2.00 0.09 4.08 0.05 0.13 0.98 1.08 0.09 1.03 0.14 0.10 1.00 0.12 0.02 1.01 0.09 1.03 0.04 0.15 0.99 0.12 0.03 2.06 0.10 1.09 0.08 3.21 1.03 1.01 0.99 0.09 2.01 0.15 0.93 0.13 1.02 0.95 0.13 1.02 0.17 1.06 0.05 0.16 3.12 0.12 0.08 2.07 1.06 1.08 1.02 0.09 0.07 0.93 0.13 1.01 0.07 0.98 0.07 1.02 0.11 0.12 0.99 0.21 1.09 1.08 0.10 3.03 0.06 0.12 1.99 0.04 0.12 1.00 0.03 0.11 1.05 1.00 0.07 0.16 1.96 0.12 0.04 2.16 1.98 1.04 0.07 0.90 0.04 0.15 1.09 3.08 0.10 1.04 0.15 0.99 0.08 1.05 0.08 1.07 0.17 0.07 1.01 0.18 2.06 0.13 0.13 2.12 1.97 0.14 0.09 0.91 0.10 1.07 1.09 3.06 1.08 0.98 0.17 0.91 0.09 0.08 3.09 0.11 1.08 0.19 0.00 2.04 0.16 2.05 0.17 0.06 2.07 0.96 2.05 0.09 0.98 0.09 0.06 2.37 0.03 0.16 1.11 0.95 0.09 1.13 0.93 4.07 0.08 0.07 0.95 1.99 0.09 0.12 1.97 1.12 0.11 0.10 2.06 0.18 0.94 0.13 0.09 1.07 0.09 1.03 0.14 0.11 0.98 0.15 1.04 0.15 0.10 1.04 2.06 0.12 1.00 0.07 0.13 2.06 0.94 0.11 0.16 1.03 0.90 0.13 1.03 0.21 1.03 1.09 0.13 2.06 0.06 0.12 1.01 0.10 0.12 1.03 0.06 4.01 0.13 0.06 1.99 
...

如果您认为有用,我不介意发送完整的文件。我怀疑这是一个简单的错误,与我对线程的假设有关,但我似乎无法发现它。感谢您抽出宝贵时间来研究这个问题。我真的很感激!

1 个答案:

答案 0 :(得分:0)

我在VC 6中尝试过你的程序,当我创建ofstream对象时,我遇到了访问冲突。以下链接帮助我解决了访问冲突。 http://www.gamedev.net/topic/73037-ofstream-access-violations-when-multi-threading/ 由于您没有提供有关违规行为的更多信息,因此我只能将其作为提示。