C++完成多線程查找文件實例。本站提示廣大學習愛好者:(C++完成多線程查找文件實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C++完成多線程查找文件實例正文
重要是多線程的互斥 文件 的查找
多線程互斥的框架
//線程函數
UINT FinderEntry(LPVOID lpParam)
{
//CRapidFinder經由過程參數傳遞出去
CRapidFinder* pFinder = (CRapidFinder*)lpParam;
CDirectoryNode* pNode = NULL;
BOOL bActive = TRUE; //bActive為TRUE,表現以後線程激活
//輪回處置m_listDir列表中的目次
while (1)
{
//從列表中掏出一個目次
::EnterCriticalSection(&pFinder->m_cs);
if (pFinder->m_listDir.IsEmpty()) //目次列表為空,以後線程不激活,所以bAactive=FALSE
{
bActive = FALSE;
}
else
{
pNode = pFinder->m_listDir.GetHead(); //獲得一個目次
pFinder->m_listDir.Remove(pNode); //從目次列表中移除
}
::LeaveCriticalSection(&pFinder->m_cs);
//假如停滯以後線程
if (bActive == FALSE)
{
//停滯以後線程
//線程數--
pFinder->m_nThreadCount--;
//假如以後運動線程數為0,跳出,停止
if (pFinder->m_nThreadCount == 0)
{
::LeaveCriticalSection(&pFinder->m_cs);
break;
}
::LeaveCriticalSection(&pFinder->m_cs);
//以後運動線程數不為0,期待其他線程向目次列表中加目次
::ResetEvent(pFinder->m_hDirEvent);
::WaitForSingleObject(pFinder->m_hDirEvent, INFINITE);
//運轉到這,就解釋其他線程叫醒了本線程
pFinder->m_nThreadCount++; //激活了本身的線程,線程數++
bActive = TRUE; //以後線程活了
continue; //跳到while,
}
//從目次列表中勝利獲得了目次
<span > </span>......................
//if (pNode)
//{
// delete pNode;
// pNode = NULL;
//}
}//end while
//促使一個搜刮線程從WaitForSingleObject前往,並加入輪回
::SetEvent(pFinder->m_hDirEvent);
//斷定此線程能否是最初一個停止輪回的線程,假如是就告訴主線程
if (::WaitForSingleObject(pFinder->m_hDirEvent,0) != WAIT_TIMEOUT)
{
::SetEvent(pFinder->m_hExitEvent);
}
return 1;
}
查找文件 的框架:
//從目次列表中勝利獲得了目次
WIN32_FIND_DATA fileData;
HANDLE hFindFile;
//生成准確的查找字符串
if (pNode->szDir[strlen(pNode->szDir)-1] != '\\')
{
strcat(pNode->szDir,"\\");
}
strcat(pNode->szDir, "*.*");
//查找文件的框架
hFindFile = ::FindFirstFile(pNode->szDir, &fileData);
if (hFindFile != INVALID_HANDLE_VALUE )
{
do
{
//假如是以後目次,跳過
if (fileData.cFileName[0] == '.')
{
continue;
}
//假如是目次
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//將以後目次參加到目次列表
。。。。。。
//使一個線程從非運動狀況釀成運動狀況
::SetEvent(pFinder->m_hDirEvent);
}
else //假如是文件
{
。。。。。。。。。。。。。
}
} while (::FindNextFile(hFindFile, &fileData));
}
一切代碼main.cpp:
#include "RapidFinder.h"
#include <stddef.h>
#include <stdio.h>
#include <process.h>
//m_nMaxThread 是const int類型,只能經由過程這類方法初始化
CRapidFinder::CRapidFinder(int nMaxThread):m_nMaxThread(nMaxThread)
{
m_nResultCount = 0;
m_nThreadCount = 0;
m_listDir.Construct(offsetof(CDirectoryNode, pNext)); //offsetof在stddef.h頭文件中
::InitializeCriticalSection(&m_cs);
m_szMatchName[0] = '\0';
m_hDirEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
m_hExitEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
}
CRapidFinder::~CRapidFinder()
{
::DeleteCriticalSection(&m_cs);
::CloseHandle(m_hDirEvent);
::CloseHandle(m_hExitEvent);
}
BOOL CRapidFinder::CheckFile(LPCTSTR lpszFileName)
{
//界說兩個字符串
char string[MAX_PATH];
char strSearch[MAX_PATH];
strcpy(string, lpszFileName);
strcpy(strSearch, m_szMatchName);
//將字符串年夜寫
_strupr(string);
_strupr(strSearch);
//比擬string中能否含有strSearch
if (strstr(string, strSearch) != NULL)
{
return TRUE;
}
return FALSE;
}
//線程函數
UINT FinderEntry(LPVOID lpParam)
{
//CRapidFinder經由過程參數傳遞出去
CRapidFinder* pFinder = (CRapidFinder*)lpParam;
CDirectoryNode* pNode = NULL;
BOOL bActive = TRUE; //bActive為TRUE,表現以後線程激活
//輪回處置m_listDir列表中的目次
while (1)
{
//從列表中掏出一個目次
::EnterCriticalSection(&pFinder->m_cs);
if (pFinder->m_listDir.IsEmpty()) //目次列表為空,以後線程不激活,所以bAactive=FALSE
{
bActive = FALSE;
}
else
{
pNode = pFinder->m_listDir.GetHead(); //獲得一個目次
pFinder->m_listDir.Remove(pNode); //從目次列表中移除
}
::LeaveCriticalSection(&pFinder->m_cs);
//假如停滯以後線程
if (bActive == FALSE)
{
//停滯以後線程
::EnterCriticalSection(&pFinder->m_cs);
pFinder->m_nThreadCount--;
//假如以後運動線程數為0,跳出,停止
if (pFinder->m_nThreadCount == 0)
{
::LeaveCriticalSection(&pFinder->m_cs);
break;
}
::LeaveCriticalSection(&pFinder->m_cs);
//以後運動線程數不為0,期待其他線程向目次列表中加目次
::ResetEvent(pFinder->m_hDirEvent);
::WaitForSingleObject(pFinder->m_hDirEvent, INFINITE);
//運轉到這,就解釋其他線程向目次列表中參加了新的目次
::EnterCriticalSection(&pFinder->m_cs);
pFinder->m_nThreadCount++; //激活了本身的線程,線程數++
::LeaveCriticalSection(&pFinder->m_cs);
bActive = TRUE; //目次不再為空
continue; //跳到while,從新在目次列表中取目次
}
//從目次列表中勝利獲得了目次
WIN32_FIND_DATA fileData;
HANDLE hFindFile;
//生成准確的查找字符串
if (pNode->szDir[strlen(pNode->szDir)-1] != '\\')
{
strcat(pNode->szDir,"\\");
}
strcat(pNode->szDir, "*.*");
//查找文件的框架
hFindFile = ::FindFirstFile(pNode->szDir, &fileData);
if (hFindFile != INVALID_HANDLE_VALUE )
{
do
{
//假如是以後目次,跳過
if (fileData.cFileName[0] == '.')
{
continue;
}
//假如是目次
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//將以後目次參加到目次列表
CDirectoryNode* p = new CDirectoryNode;
strncpy(p->szDir, pNode->szDir, strlen(pNode->szDir)-3); //將pNode前面的*.*三位去失落
strcat(p->szDir, fileData.cFileName);
::EnterCriticalSection(&pFinder->m_cs);
pFinder->m_listDir.AddHead(p);
::LeaveCriticalSection(&pFinder->m_cs);
// 如今的p剛參加列表,就要delete,確定會失足
//delete p;
//p = NULL;
//使一個線程從非運動狀況釀成運動狀況
::SetEvent(pFinder->m_hDirEvent);
}
else //假如是文件
{
//斷定能否為要查找的文件
if (pFinder->CheckFile(fileData.cFileName)) //相符查找的文件
{
//打印
::EnterCriticalSection(&pFinder->m_cs);
pFinder->m_nResultCount++;
::LeaveCriticalSection(&pFinder->m_cs);
printf("find %d:%s\n", pFinder->m_nResultCount, fileData.cFileName);
}
}
} while (::FindNextFile(hFindFile, &fileData));
}
//if (pNode)
//{
// delete pNode;
// pNode = NULL;
//}
}//end while
//促使一個搜刮線程從WaitForSingleObject前往,並加入輪回
::SetEvent(pFinder->m_hDirEvent);
//斷定此線程能否是最初一個停止輪回的線程,假如是就告訴主線程
if (::WaitForSingleObject(pFinder->m_hDirEvent,0) != WAIT_TIMEOUT)
{
::SetEvent(pFinder->m_hExitEvent);
}
return 1;
}
void main()
{
printf("start:\n");
CRapidFinder* pFinder = new CRapidFinder(64);
CDirectoryNode* pNode = new CDirectoryNode;
char szPath[] = "c:\\";
char szFile[] = "config";
strcpy(pNode->szDir, szPath);
pFinder->m_listDir.AddHead(pNode);
strcpy(pFinder->m_szMatchName, szFile);
pFinder->m_nThreadCount = pFinder->m_nMaxThread;
//開端開啟多線程
for (int i=0;i< pFinder->m_nMaxThread;i++)
{
::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)FinderEntry, pFinder, 0, NULL);
}
//只要m_hExitEvent受信狀況,主線程才恢復運轉
::WaitForSingleObject(pFinder->m_hExitEvent,INFINITE);
printf("共找到%d\n", pFinder->m_nResultCount);
//if (pNode != NULL) delete pNode;
if (pFinder != NULL) delete pFinder;
getchar();
return;
}
rapidfinder.h文件以下:
#include "_AFXTLS_.H"
struct CDirectoryNode: public CNoTrackObject
{
CDirectoryNode* pNext;
char szDir[MAX_PATH];
};
class CRapidFinder
{
public:
CRapidFinder(int nMaxThread); //結構函數
virtual ~CRapidFinder(); //析構函數
BOOL CheckFile(LPCTSTR lpszFileName); //檢討lpszFileName能否相符查找前提
int m_nResultCount; //找到的成果數目
int m_nThreadCount; //以後的線程數目
CTypedSimpleList<CDirectoryNode*> m_listDir; //查找目次
CRITICAL_SECTION m_cs; //同享
const int m_nMaxThread; //最年夜線程數目
char m_szMatchName[MAX_PATH]; //要查找的稱號
HANDLE m_hDirEvent; //添加新目次後置位
HANDLE m_hExitEvent; //一切線程加入時置位
};
上面這兩個類就是完成了simplelist類和模板
_afxatl.cpp文件:
#include "_AFXTLS_.H"
void CSimpleList::AddHead(void* p)
{
*GetNextPtr(p) = m_pHead;
m_pHead = p;
}
BOOL CSimpleList::Remove(void* p)
{
if (p == NULL)
{
return FALSE;
}
BOOL bResult = FALSE;
if (p == m_pHead)
{
m_pHead = *GetNextPtr(m_pHead);
bResult = TRUE;
}
else
{
void* pTest = m_pHead;
while (pTest != NULL && *GetNextPtr(pTest) != p)
{
pTest = *GetNextPtr(pTest);
}
if (pTest != NULL)
{
*GetNextPtr(pTest) = *GetNextPtr(p);
bResult = TRUE;
}
}
return bResult;
}
void* CNoTrackObject::operator new(size_t nSize)
{
void* p = ::GlobalAlloc(GPTR, nSize);
return p;
}
void CNoTrackObject::operator delete(void* p)
{
if (p!=NULL)
{
::GlobalFree(p);
}
}
afxatl.h文件:
#ifndef _AFXTLS_H_H
#define _AFXTLS_H_H
#include <Windows.h>
class CSimpleList
{
public:
CSimpleList(int nNextOffset=0);
void Construct(int nNextOffset);
BOOL IsEmpty() const;
void AddHead(void* p);
void RemoveAll();
void* GetHead() const;
void* GetNext(void* p) const;
BOOL Remove(void* p);
//為完成接口所須要的成員
void* m_pHead;
int m_nNextOffset;
void** GetNextPtr(void* p) const;
};
//類的內聯函數
inline CSimpleList::CSimpleList(int nNextOffset)
{m_pHead = NULL; m_nNextOffset = nNextOffset;}
inline void CSimpleList::Construct(int nNextOffset)
{m_nNextOffset = nNextOffset;}
inline BOOL CSimpleList::IsEmpty() const
{return m_pHead==NULL;}
inline void CSimpleList::RemoveAll()
{m_pHead=NULL;}
inline void* CSimpleList::GetHead() const
{return m_pHead;}
inline void* CSimpleList::GetNext(void* preElement) const
{
return *GetNextPtr(preElement);
}
inline void** CSimpleList::GetNextPtr(void* p) const
{
return (void**)((BYTE*)p + m_nNextOffset);
}
class CNoTrackObject
{
public:
void* operator new(size_t nSize);
void operator delete(void*);
virtual ~CNoTrackObject(){};
};
template<class TYPE>
class CTypedSimpleList:public CSimpleList
{
public:
CTypedSimpleList(int nNextOffset=0)
:CSimpleList(nNextOffset){}
void AddHead(TYPE p)
{
CSimpleList::AddHead((void*)p);
}
TYPE GetHead()
{
return (TYPE)CSimpleList::GetHead();
}
TYPE GetNext(TYPE p)
{
return (TYPE)CSimpleList::GetNext((void*)p);
}
BOOL Remove(TYPE p)
{
return CSimpleList::Remove(p);
}
operator TYPE()
{
return (TYPE)CSimpleList::GetHead();
}
};
#endif
願望本文所述對年夜家的C++法式設計有所贊助。