twerdster
twerdster

Reputation: 5027

C++ windows threading and mutex issue

I am a bit rusty with threaded programs especially in windows. I have created a simple mex file in Matlab that is meant to read a number of files with each file being read in its own thread. The file doesnt do anything really useful but is a precursor to a more complicated version that will use all of the functionality ive put into this file. Here is the code:

#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);

I compile it like this in Matlab:

mex readFile.cpp

And then run it like this:

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

The problem is that when I set nFiles to be less than or equal to 64 everything works as expected and I get the following output:

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

However when I set nFiles to 65 or larger I get:

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.

I have also tested it without threading and it works fine.

I cannot see what Im doing wrong or why the cutoff to using the mutex would be so arbitrary so I am assuming there is something I am not taking into account. Can anyone see where I have a blatant mistake relating to the error Im seeing?

Upvotes: 1

Views: 1000

Answers (1)

slugonamission
slugonamission

Reputation: 9642

In the documentation for WaitForMultipleObjects, "The maximum number of object handles is MAXIMUM_WAIT_OBJECTS.", which is 64 on most systems.

This is also (almost) a duplicate of this thread. The summary is really just that yes, the limit is 64, and also to use the information in the remarks section of WaitForMultipleObjects to build up a tree of threads to wait on.

Upvotes: 3

Related Questions