title: Create multiple files using multi threading in C++ using Win32 API
body: <p>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?</p>
  
  <pre><code> #include "stdafx.h"
      #include &lt;Windows.h&gt;
      #include &lt;Strsafe.h&gt;
      #include &lt;WinBase.h&gt;
  
  
          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(&amp;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 &lt;= BUFFER_SIZE; i++)
              {
                  ReadBuffer[i] = (char)(i%26 + 'a');
              }
              for (i = 0; i &lt;= filesizeinKB; i++)
              {
                  SetLastError(0);
                  bError= WriteFile(ofile, ReadBuffer-1, BUFFER_SIZE,&amp;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(&amp;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(&amp;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), &amp;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(&amp;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 (&amp;section);
              CreateDirectory(drive_Name,NULL);
  
              for(i=0;i&lt;5;i++)
              {
                  SetLastError(0);
                  StringCchPrintf(filename,bsize, TEXT("%s\\file_%d.txt"),drive_Name,i );
                  hfile=CreateFileW(filename,GENERIC_WRITE|GENERIC_READ,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, &amp;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, &amp;ReadthreadID);
                  SetThreadPriority(read_Handle[i],1);
                  _tprintf(_T("read thread id is %d\n "),ReadthreadID);
                  check_Handle =CreateThread(0, 0, check_Thread,(void*)&amp;i ,0,&amp;OpenThreadID);
              }   
              for (i=0; i&lt;count; ++i)
              {
                  WaitForSingleObject(Write_Handle[i],INFINITE); 
                  if ( !GetExitCodeThread(Write_Handle, &amp;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, &amp;exThread) ) 
                  {  
                      _tprintf(_T("GetExitCodeThread %08X\n"),GetLastError()); 
                  } 
                  SetLastError(0);
                  CloseHandle(read_Handle[i]);
                  _tprintf(_T("GetExitCodeThread %08X\n"),GetLastError()); 
  
                  CloseHandle(check_Thread);
  
              }
  
              DeleteCriticalSection(&amp;section);
              return 1;
  
          }
  </code></pre>