jeudi 20 août 2015

How to work correctly with the WaitForMultipleObjects?

I've a problem about concurrent programming in Visual Studio C++. In the main, in a for cycle i call 38 times the function _beginthreadex similar to the CreateThread. Each thread should work on a different file, and all is ok also for the synchronization. The problem is the following: in the main, after the WaitForMultipleObjects saying to wait ALL thread (the boolean is true). The behaviour, however, is the following: a variable number of threads complete their work, after that it could happen that the WaitForMultipleObjects return and the main program end calling ExitProcess(), and part of the work is incomplete. I "solved" with a Sleep function before the wait to give to all thread the possibility to complete their work, but it is not a real solution! Any suggestion?

MAIN:

[...]
xPloreFS(path, str, o);
Sleep(10000);
DWORD xxx = WaitForMultipleObjects(38, &thx[0].v, true, INFINITE);
for (int ss = 0; ss < MAX_HANDLE; ss++)
    CloseHandle(thx[i].v);
cout << "finito" << endl;
return 0;

xPloreFS function:

hfind = FindFirstFile(wstring(path).append(L"/*").c_str(), &findFileData);
while (hfind != INVALID_HANDLE_VALUE) {
    if (findFileData.cFileName != wstring(L".") && findFileData.cFileName != wstring(L"..") && findFileData.cFileName != wstring(L"output.txt")) {
        //wcout << findFileData.cFileName << " ";

        if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
[...]
xPloreFS(p, str, out);

else {
[...]

thx[i].v = (HANDLE)_beginthreadex(NULL, 0, pointerF, (void*)kl, 0, NULL); //nn posso passargli un reference come &i! if (thx[i].v == NULL) cout << " ERROR " << endl; i++; }

And finally there is the function called by the threadbegin, I don't put the code

Aucun commentaire:

Enregistrer un commentaire