开发者

Create multiple files using multi threading in C++ using Win32 API

I am trying to create files in a pen drive. If the pen drive is not present, threads should suspend. When it is inserted it should resume. Here is the code I tried but it's not working properly. Can any one help me?

 #include "stdafx.h"
    #include <Windows.h>
    #include <Strsafe.h>
    #include <WinBase.h>


        LPCTSTR Disk=L"E:\";

        LPTSTR drive_Name=L"E:\\demo";

        CRITICAL_SECTION section;


         #define BUFFER_SIZE 1024
         #define count 10

        HANDLE Write_Handle[10],Open_Handle[10],read_Handle[10] ;

        DWORD WINAPI check_Thread(LPVOID lpParameter)

        {

        int *nThreadNo = (int*)lpParameter;

        while(1)

        {

                if(GetDiskFreeSpaceExW(Disk,NULL,NULL,NULL))

                {

                    ResumeThread(Write_Handle[*nThreadNo]);
                    ResumeThread(read_Handle[*nThreadNo]);
                }
                else
                {
                    SuspendThread(Write_Handle[*nThreadNo]);
                    SuspendThread(read_Handle[*nThreadNo]);
                }
            }

        }


        DWORD  WINAPI Write_Thread(LPVOID lpParameter)
        {
            DWORD g_tid = GetCurrentThreadId();
            _tprintf(_T(" write thread id %d\n"),g_tid);
            LPCWSTR  filename=(LPCWSTR)lpParameter;
            HANDLE ofile;
            EnterCriticalSection(&section);
            ofile=CreateFileW(filename,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,NULL, NULL);
            int d;
                d=GetLastError();
                if(ERROR_SUCCESS!=GetLastError())
                {
                    _tprintf(_T("error values in open thread %d \n"),GetLastError());
                    _tprintf(_T("filename %s \n"),filename);
                }

            const int filesizeinKB = 1024;
            BOOL bError;
            DWORD dwBytesWritten=0;
             WCHAR ReadBuffer[BUFFER_SIZE] = {0};
            int i;


            for(i = 0; i <= BUFFER_SIZE; i++)
            {
                ReadBuffer[i] = (char)(i%26 + 'a');
            }
            for (i = 0; i <= filesizeinKB; i++)
            {
                SetLastError(0);
                bError= WriteFile(ofile, ReadBuffer-1, BUFFER_SIZE,&dwBytesWritten, NULL);
                bError=GetLastError();

                if (ERROR_SUCCESS!=GetLastError())
                {   _tprintf(_T("error value in write %d\n"),GetLastError());
                    _tprintf(_T(" Write Error...\n"));
                        return 1;
                }

            }
            SetLastError(0);
            CloseHandle(ofile);
            _tprintf(_T("write close error values %d\n"),GetLastError());
            LeaveCriticalSection(&section);
            return 1;
        }


        DWORD WINAPI Read_Thread(LPVOID lpParameter)
        {
            HANDLE ofile;
            DWORD g_tid = GetCurrentThreadId();
            _tprintf(_T(" write thread id %d\n"),g_tid);
            LPCWSTR  filename=(LPCWSTR)lpParameter;
            EnterCriticalSection(&section);
            ofile=CreateFileW(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,NULL, NULL);
            int d;
                d=GetLastError();
                if(ERROR_SUCCESS!=GetLastError())
                {
                    _tprintf(_T("error values in open thread %d \n"),GetLastError());
                    _tprintf(_T("filename %s \n"),filename);
                }

            DWORD dwBytesRead=0;
             WCHAR ReadBuffer[BUFFER_SIZE] = {0};
            _tprintf(_T(" read thread \n"));
            SetLastError(0);
            int err;

            ReadFile(ofile, ReadBuffer, (BUFFER_SIZE-1), &dwBytesRead, NULL);

                err=GetLastError();
                _tprintf(_T("read error values %d \n"),GetLastError());
                if(ERROR_SUCCESS!=GetLastError())
                {
                    _tprintf(L"reading failed \n");
                    return 0;
                }
                SetLastError(0);
                CloseHandle(ofile);
                err=GetLastError();
                _tprintf(_T("close error values %d\n"),GetLastError());
                LeaveCriticalSection(&section);
                return 1;
        }


        int _tmain(int argc, _TCHAR* argv[])
        {

            unsigned int myCounter = 0;
            DWORD WritethreadID,OpenThreadID,ReadthreadID;
            HANDLE check_Handle;
             DWORD exThread;
            TCHAR filename[100];
            HANDLE hfile;
            INT bsize=100;
            int i=0;

            InitializeCriticalSection (&section);
            CreateDirectory(drive_Name,NULL);

            for(i=0;i<5;i++)
            {
                SetLastError(0);
                StringCchPrintf(filename,bsize, TEXT("%s\\file_%d.txt"),drive_Name,i );
                hfile=CreateFileW(filename,GENERIC_WRITE|GENERIC_RE开发者_如何学PythonAD,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

                if (hfile == INVALID_HANDLE_VALUE) 
                { 
                    _tprintf(_T("invalid handle \n" ));

                }
                _tprintf(_T("file created %s\n"),filename);
                CloseHandle(hfile);

                Write_Handle[i] =CreateThread(0, 0, Write_Thread, filename, CREATE_SUSPENDED, &WritethreadID);
                SetThreadPriority(Write_Handle[i],2);
                _tprintf(_T("write thread id is %d\n"),WritethreadID);
                read_Handle[i]=CreateThread(0, 0, Read_Thread, filename, CREATE_SUSPENDED, &ReadthreadID);
                SetThreadPriority(read_Handle[i],1);
                _tprintf(_T("read thread id is %d\n "),ReadthreadID);
                check_Handle =CreateThread(0, 0, check_Thread,(void*)&i ,0,&OpenThreadID);
            }   
            for (i=0; i<count; ++i)
            {
                WaitForSingleObject(Write_Handle[i],INFINITE); 
                if ( !GetExitCodeThread(Write_Handle, &exThread) ) 
                {  
                    _tprintf(_T("close thread %08X\n"),GetLastError()); 

                }
                SetLastError(0);
                CloseHandle(Write_Handle[i]);
                _tprintf(_T("close thread %08X\n"),GetLastError()); 
                WaitForSingleObject(read_Handle[i],INFINITE); 
                if ( !GetExitCodeThread(read_Handle, &exThread) ) 
                {  
                    _tprintf(_T("GetExitCodeThread %08X\n"),GetLastError()); 
                } 
                SetLastError(0);
                CloseHandle(read_Handle[i]);
                _tprintf(_T("GetExitCodeThread %08X\n"),GetLastError()); 

                CloseHandle(check_Thread);

            }

            DeleteCriticalSection(&section);
            return 1;

        }


If you have open file handles to a USB drive when that drive is removed, those file handles will become invalid. Reinserting the USB drive will not reconstitute those handles in such a way that you can continue after resuming a thread.

You will need to:

  • detect when the device is removed, and close those now-broken handles
  • when the device is inserted, open the files again and continue whatever you were doing


Your error handlers are returning without calling LeaveCriticalSection, leaving the CS locked and blocking the other threads indefinitely. From the EnterCriticalSection docs:

  • If a thread terminates while it has ownership of a critical section, the state of the critical section is undefined.

Those cases also leave the file handles open.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜