C ++ windows threading和mutex问题

时间:2011-11-15 01:19:21

标签: c++ windows matlab mutex

我对线程程序有点生疏,特别是在windows中。 我在Matlab中创建了一个简单的mex文件,用于读取大量文件,每个文件都在自己的线程中读取。 该文件没有做任何真正有用的事情,但它是一个更复杂版本的前身,它将使用我放入此文件的所有功能。 这是代码:

#include <windows.h>
#include "mex.h"
#include <fstream>

typedef unsigned char uchar;
typedef unsigned int uint;

using namespace std;

int N;
int nThreads;
const int BLOCKSIZE = 1024;
char * buffer;
char * out;
HANDLE    hIOMutex;

DWORD WINAPI runThread(LPVOID argPos) {
    int pos = *(reinterpret_cast<int*>(argPos));

    DWORD dwWaitResult = WaitForSingleObject( hIOMutex, INFINITE );

    if (dwWaitResult == WAIT_OBJECT_0){
        char buf[20];
        sprintf(buf, "test%i.dat", pos);
        ifstream ifs(buf, ios::binary);

        if (!ifs.fail()) {
            mexPrintf("Running thread:%i\n", pos);
            for (int i=0; i<N/BLOCKSIZE;i++) {
                if (ifs.eof()){ 
                    mexPrintf("File %s exited at i=%i\n", buf, (i-1)*BLOCKSIZE);
                    break;
                }
                ifs.read(&buffer[pos*BLOCKSIZE], BLOCKSIZE);
            }
        }
        else {
            mexPrintf("Could not open file %s\n", buf);
        }

        ifs.close();
        ReleaseMutex( hIOMutex);
    }
    else
        mexPrintf("The Mutex failed in thread:%i \n", pos);


    return TRUE;
}

// 0 - N is data size
// 1 - nThreads is number of threads
// 2 - this is the output array

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) {
    N = mxGetScalar(prhs[0]);
    nThreads = mxGetScalar(prhs[1]);
    out = (char*)mxGetData(prhs[2]);
    buffer = (char*)malloc(BLOCKSIZE*nThreads);
    hIOMutex= CreateMutex(NULL, FALSE, NULL);

    HANDLE *hArr = (HANDLE*)malloc(sizeof(HANDLE)*nThreads);
    int *tInd = (int*)malloc(sizeof(int)*nThreads);

    for (int i=0;i<nThreads;i++){
        tInd[i]=i;
        hArr[i] = CreateThread( NULL, 0, runThread, &tInd[i], 0, NULL);
        if (!hArr[i]) {
            mexPrintf("Failed to start thread:%i\n", i);
            break;
        }
    }

    WaitForMultipleObjects( nThreads, hArr, TRUE, INFINITE);

    for (int i=0;i<nThreads;i++)
        CloseHandle(hArr[i]);

    CloseHandle(hIOMutex);
    mexEvalString("drawnow");
    mexPrintf("Finished all threads.\n");

    free(hArr);
    free(tInd);
    free(buffer);

我在Matlab中编译它:

mex readFile.cpp

然后像这样运行:

out = zeros(1024*1024,1,'uint8'); 
readFile(1024*1024,nFiles,out);

问题在于,当我将nFiles设置为小于或等于64时,一切都按预期工作,我得到以下输出:

Running thread:0
.
.
.
Running thread:62
Running thread:63
Finished all threads.

然而,当我将nFiles设置为65或更大时,我得到:

Running thread:0
Running thread:1
Running thread:2
Running thread:3
The Mutex failed in thread:59 
The Mutex failed in thread:60 
The Mutex failed in thread:61 
.
.
.
(up to nFiles-1)
Finished all threads.

我也在没有线程的情况下对其进行了测试,并且工作正常。

我看不出我做错了什么,或为什么使用互斥锁的截止是如此随意,所以我假设有一些我没有考虑到的东西。 任何人都可以看到我在哪里遇到与我看到的错误有关的明显错误?

1 个答案:

答案 0 :(得分:3)

WaitForMultipleObjects的文档中,“最大对象句柄数为MAXIMUM_WAIT_OBJECTS。”,在大多数系统上为64。

这也是(几乎)this thread的副本。摘要实际上是肯定的,限制是64,并且还使用WaitForMultipleObjects的备注部分中的信息来构建要等待的线程树。