Quick Retrieves with Multithreading Realization Files under VC

zhaozj2021-02-16  151

The use of multithreading file under the VC quickly retrieve 2004-07-30 ■ Author: Feng Jie ■ Source: NEW YORK

The operating system generally provides the function of file search, but the sequential search is used, and the search efficiency is very low. And according to this approach, it is very cumbersome, and it is often difficult to deal with in the catalog level. This paper uses multi-threaded technology to realize the fast search of documents, with very small amounts of code, and high execution efficiency.

The basic idea of ​​multi-threaded file searches this article is very simple, that is, find a directory to open a thread, and find a file. Of course, it will be handled in the thread, so that synchronous searches.

The following describes the specific implementation under the VC platform:

1. Search for two functions for two Win32

Handle FindfirstFile (LPCTSTSTSTR LPFILENAME, LPWIN32_FIND_DATA LPFINDFILEDATA); BOOL FINDNEXTFILE (Handle Hfindfile, LPWIN32_FIND_DATA LPFINDFILEDATA);

2. Establish a thread function First to define the parameter structure of the thread for the delivery of file information:

Typedef struct tagthreadparam {cstring strpath; cstring strfilename;} threadparam m_param;

Since the thread is to manipulate global variables, the mutex is defined:

CMUTEX M_MUTEXTHREADCOUNT, M_MUTEXTHREADPARAM, M_MUTEXPATH;

If the search is finished or the search is terminated, there must be an event notification, so create an event:

CEVENT M_EVENT (false, false, null, null); uint uthreadcount = 0; // generated thread number cstringArray m_strpatharray; // Store Searching file path number // thread function uint getFilePaththreadProc (lpvoid pParam) {if (pParam = = NULL) AfxEndThread (NULL); tHREADPARAM * m_pParam = (tHREADPARAM *) pParam; CString strPath = m_pParam-> strPath; CString strFileName = m_pParam-> strFileName; m_mutexThreadCount.Lock (); uThreadCount ; m_mutexThreadCount.Unlock (); HANDLE hFile ; WIN32_FIND_DATA * pInfo = new WIN32_FIND_DATA; hFile = :: FindFirstFile (strPath "//*.*", pInfo); if (hFile == INVALID_HANDLE_VALUE) {delete pInfo; m_mutexThreadCount.Lock (); uThreadCount--; // All The thread is complete, then activate the event, notify the application to complete the search (UTHREADCOUNT == 0) m_Event.sevent (); m_mutexthreadcount.unlock (); return- 0;} do {i (Pinfo-> cfilename [0 '.'] == '') continue; char cFileName [MAX_PATH]; strcpy (cFileName, pInfo-> cFileName); CString strFile = cFileName; if (pInfo-> dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {// If a directory, Open up new search thread m_mutexthreadparam.lock (); m_param. strPath = strPath "//" strFile; m_param.strFileName = strFileName; AfxBeginThread (GetFilePathThreadProc, & m_param, THREAD_PRIORITY_NORMAL); m_mutexThreadParam.Unlock ();} else {// If the file is directly compare the file to be searched if (strFile == strFileName) {m_mutexPath.Lock (); m_strPathArray.Add (strPath "//" strFile); m_mutexPath.Unlock (); m_mutexThreadCount.Lock (); uThreadCount--; if (uThreadCount == 0) m_event.SetEvent (); M_mutexthreadcount.unlock (); return 0;

}}} While (:: FindNextFile (hFile, pInfo)); :: FindClose (hFile); delete pInfo; m_mutexThreadCount.Lock (); uThreadCount--; if (uThreadCount == 0) m_event.SetEvent (); m_mutexThreadCount. Unlock (); return 0;} 3. Call the thread function to perform a search

void GetFilePath () {m_event.ResetEvent (); // set the non-signal state event uThreadCount = 0; m_strPathArray.RemoveAll (); m_param.strPath = m_strPath; m_param.strFileName = m_strFileName; m_param.m_pListInfo = & m_ListInfo; // start a thread AfxBeginThread (GetFilePathThreadProc, & m_param, THREAD_PRIORITY_NORMAL); // wait for the search to complete or a termination event occurs :: WaitForSingleObject (m_event.m_hObject, INFINITE); if (m_strPathArray.GetUpperBound () == - 1) {AfxMessageBox ( "not found File ", MB_OK | MB_ICONICONITION; RETURN;} for (int i = 0; i

4. If you want to stop searching in the middle, you only need to store thread objects to a thread object array. Of course, add a thread object to an array every additional thread, and remove the thread object from the array. This allows these thread objects to achieve the purpose when you want to terminate the search. Detact call two functions:

BOOL GetExitCodeThread (HANDLE hThread, LPDWORD lpExitCode); BOOL TerminateThread (HANDLE hThread, DWORD dwExitCode); BOOL GetExitCodeThread (HANDLE hThread, // handle to the thread LPDWORD lpExitCode // address to receive termination status); BOOL TerminateThread (HANDLE hThread, / / Handle to Thread DWORD DWEXITCODE / / EXIT code

Practice results prove that using multi-threaded technology to implement file search, greatly improve the efficiency of the program. This article compiles through the VC 6.0 and Windows2000 environments.

转载请注明原文地址:https://www.9cbs.com/read-8797.html

New Post(0)