C++實現(xiàn)多線程查找文件實例
主要是多線程的互斥 文件 的查找
多線程互斥的框架
UINT FinderEntry(LPVOID lpParam)
{
//CRapidFinder通過參數(shù)傳遞進來
CRapidFinder* pFinder = (CRapidFinder*)lpParam;
CDirectoryNode* pNode = NULL;
BOOL bActive = TRUE; //bActive為TRUE,表示當(dāng)前線程激活
//循環(huán)處理m_listDir列表中的目錄
while (1)
{
//從列表中取出一個目錄
::EnterCriticalSection(&pFinder->m_cs);
if (pFinder->m_listDir.IsEmpty()) //目錄列表為空,當(dāng)前線程不激活,所以bAactive=FALSE
{
bActive = FALSE;
}
else
{
pNode = pFinder->m_listDir.GetHead(); //得到一個目錄
pFinder->m_listDir.Remove(pNode); //從目錄列表中移除
}
::LeaveCriticalSection(&pFinder->m_cs);
//如果停止當(dāng)前線程
if (bActive == FALSE)
{
//停止當(dāng)前線程
//線程數(shù)--
pFinder->m_nThreadCount--;
//如果當(dāng)前活動線程數(shù)為0,跳出,結(jié)束
if (pFinder->m_nThreadCount == 0)
{
::LeaveCriticalSection(&pFinder->m_cs);
break;
}
::LeaveCriticalSection(&pFinder->m_cs);
//當(dāng)前活動線程數(shù)不為0,等待其他線程向目錄列表中加目錄
::ResetEvent(pFinder->m_hDirEvent);
::WaitForSingleObject(pFinder->m_hDirEvent, INFINITE);
//運行到這,就說明其他線程喚醒了本線程
pFinder->m_nThreadCount++; //激活了自己的線程,線程數(shù)++
bActive = TRUE; //當(dāng)前線程活了
continue; //跳到while,
}
//從目錄列表中成功取得了目錄
<span style="white-space:pre"> </span>......................
//if (pNode)
//{
// delete pNode;
// pNode = NULL;
//}
}//end while
//促使一個搜索線程從WaitForSingleObject返回,并退出循環(huán)
::SetEvent(pFinder->m_hDirEvent);
//判斷此線程是否是最后一個結(jié)束循環(huán)的線程,如果是就通知主線程
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
{
//如果是當(dāng)前目錄,跳過
if (fileData.cFileName[0] == '.')
{
continue;
}
//如果是目錄
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//將當(dāng)前目錄加入到目錄列表
。。。。。。
//使一個線程從非活動狀態(tài)變成活動狀態(tài)
::SetEvent(pFinder->m_hDirEvent);
}
else //如果是文件
{
。。。。。。。。。。。。。
}
} while (::FindNextFile(hFindFile, &fileData));
}
所有代碼main.cpp:
#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;
}
//線程函數(shù)
UINT FinderEntry(LPVOID lpParam)
{
//CRapidFinder通過參數(shù)傳遞進來
CRapidFinder* pFinder = (CRapidFinder*)lpParam;
CDirectoryNode* pNode = NULL;
BOOL bActive = TRUE; //bActive為TRUE,表示當(dāng)前線程激活
//循環(huán)處理m_listDir列表中的目錄
while (1)
{
//從列表中取出一個目錄
::EnterCriticalSection(&pFinder->m_cs);
if (pFinder->m_listDir.IsEmpty()) //目錄列表為空,當(dāng)前線程不激活,所以bAactive=FALSE
{
bActive = FALSE;
}
else
{
pNode = pFinder->m_listDir.GetHead(); //得到一個目錄
pFinder->m_listDir.Remove(pNode); //從目錄列表中移除
}
::LeaveCriticalSection(&pFinder->m_cs);
//如果停止當(dāng)前線程
if (bActive == FALSE)
{
//停止當(dāng)前線程
::EnterCriticalSection(&pFinder->m_cs);
pFinder->m_nThreadCount--;
//如果當(dāng)前活動線程數(shù)為0,跳出,結(jié)束
if (pFinder->m_nThreadCount == 0)
{
::LeaveCriticalSection(&pFinder->m_cs);
break;
}
::LeaveCriticalSection(&pFinder->m_cs);
//當(dāng)前活動線程數(shù)不為0,等待其他線程向目錄列表中加目錄
::ResetEvent(pFinder->m_hDirEvent);
::WaitForSingleObject(pFinder->m_hDirEvent, INFINITE);
//運行到這,就說明其他線程向目錄列表中加入了新的目錄
::EnterCriticalSection(&pFinder->m_cs);
pFinder->m_nThreadCount++; //激活了自己的線程,線程數(shù)++
::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
{
//如果是當(dāng)前目錄,跳過
if (fileData.cFileName[0] == '.')
{
continue;
}
//如果是目錄
if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//將當(dāng)前目錄加入到目錄列表
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);
// 現(xiàn)在的p剛加入列表,就要delete,肯定會出錯
//delete p;
//p = NULL;
//使一個線程從非活動狀態(tài)變成活動狀態(tài)
::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返回,并退出循環(huán)
::SetEvent(pFinder->m_hDirEvent);
//判斷此線程是否是最后一個結(jié)束循環(huán)的線程,如果是就通知主線程
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受信狀態(tài),主線程才恢復(fù)運行
::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文件如下:
struct CDirectoryNode: public CNoTrackObject
{
CDirectoryNode* pNext;
char szDir[MAX_PATH];
};
class CRapidFinder
{
public:
CRapidFinder(int nMaxThread); //構(gòu)造函數(shù)
virtual ~CRapidFinder(); //析構(gòu)函數(shù)
BOOL CheckFile(LPCTSTR lpszFileName); //檢查lpszFileName是否符合查找條件
int m_nResultCount; //找到的結(jié)果數(shù)量
int m_nThreadCount; //當(dāng)前的線程數(shù)量
CTypedSimpleList<CDirectoryNode*> m_listDir; //查找目錄
CRITICAL_SECTION m_cs; //共享
const int m_nMaxThread; //最大線程數(shù)量
char m_szMatchName[MAX_PATH]; //要查找的名稱
HANDLE m_hDirEvent; //添加新目錄后置位
HANDLE m_hExitEvent; //所有線程退出時置位
};
下面這兩個類就是實現(xiàn)了simplelist類和模板
_afxatl.cpp文件:
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文件:
#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);
//為實現(xiàn)接口所需要的成員
void* m_pHead;
int m_nNextOffset;
void** GetNextPtr(void* p) const;
};
//類的內(nèi)聯(lián)函數(shù)
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++程序設(shè)計有所幫助。
欄 目:C語言
下一篇:C++使用CriticalSection實現(xiàn)線程同步實例
本文標(biāo)題:C++實現(xiàn)多線程查找文件實例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3269.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10如何判斷一個數(shù)是否為2的冪次方?若是,并判斷出來是多少次方
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10如何判斷一個數(shù)是否為4的冪次方?若是,并判斷出來是多少次方
- 01-10c++中inline的用法分析


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什