I have written a code that takes directory as input and outputs list of files in it.I implemented it in single thread.what to do in order to implement using multiple threads? provide me logic or code.
Platform : Windows API: Windows.h Language: c++
#include <iostream>
#include <Windows.h>
#include <queue>
#include <TlHelp32.h>
#include <string>
#include <vector>
using namespace std;
#define MAX_THREADS 1
int Files = 0;
vector<string>files;
vector<string> ListContents(string path,vector<string>&files)
{
HANDLE hfind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAA ffd;
string spec;
wstring ws;
deque<string> directories;
directories.push_back(path);
files.clear();
while(!directories.empty())
{
path = directories.back();
spec = path + "\\" + "*";
directories.pop_back();
std::wstring wspec( spec.begin(),spec.end());
ws = wspec;
hfind = FindFirstFileA(spec.c_str(),&ffd);
if(hfind == INVALID_HANDLE_VALUE)
continue;
//return false;
do
{
if(strcmp(ffd.cFileName,".") && strcmp(ffd.cFileName,".."))
{
if(ffd.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
directories.push_back(path + "\\" + ffd.cFileName );
}
else
{
//cout<<"Current File is "<<ffd.cFileName<<endl;
files.push_back(path + "\\" + ffd.cFileName);
Files++;
}
}
}while(FindNextFileA(hfind,&ffd)!=0);
if (GetLastError() != ERROR_NO_MORE_FILES) {
FindClose(hfind);
ExitProcess(4);
//return false;
}
FindClose(hfind);
hfind = INVALID_HANDLE_VALUE;
}
//return true;
return files;
}
void display(vector<string>&files)
{
vector<string>::iterator iter = files.begin();
if(files.size()!=0)
do
{
cout<<*iter<<endl;
*iter++;
}while(iter!=files.end());
}
DWORD WINAPI MyThread1(LPVOID s)
{
char* ss = (char*)s;
string path(ss);
//vector<string> files;
files = ListContents(path,files);
//display(files);
return 0;
}
int main(int argc,char *argv[])
{
DWORD threadid;
int newent = 0;
int newex = 0;
FILETIME creation,exit,kernel,user;
SYSTEMTIME st1,st2;
THREADENTRY32 entry;
char szEntrytime[255],szExittime[255];
//vector<string> files;
Sem = CreateSemaphore(NULL,MAX_SEM_COUNT,MAX_SEM_COUNT,NULL);
for(int i= 0;i<MAX_THREADS;i++)
{
hthread[i] = CreateThread(NULL,0,List,NULL,0,&threadid);
if( hthread[i] == NULL )
{
printf("CreateThread error: %d\n", GetLastError());
return 1;
}
}
HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,0);
if(hthread)
{
WaitForMultipleObjects(MAX_THREADS,&hthread,TRUE,INFINITE);
if(GetThreadTimes(hthread,&creation,&exit,&kernel,&user)==0)
{
cout<<"can't able to get thrad timings";
ExitProcess(3);
}
else
{
GetThreadTimes(hthread,&creation,&exit,&kernel,&user);
FileTimeToLocalFileTime(&creation,&creation);
FileTimeToLocalFileTime(&exit,&exit);
FileTimeToSystemTime(&creation,&st1);
FileTimeToSystemTime(&exit,&st2);
}
}
GetTimeFormatA( LOCALE_USER_DEFAULT, 0, &st1, NULL, szEntrytime, 255 );
GetTimeFormatA( LOCALE_USER_DEFAULT, 0, &st2, NULL, szExittime, 255 );
printf( "Thread Entry Time %s\n", szEntrytime );
printf( "Thread Exit Time %s\n", szExittime );
//files = ListContents(argv[1],files);
cout<<" No of Files "<<Files <<endl ;
CloseHandle(hthread);
display(files);
/*if(ListContents(argv[1],files))
{
}*/
system("pause");
return 0;
}