Quick Retrieves with Multithreading Realization Files under VC

xiaoxiao2021-03-06  61

Http://www.yesky.com/softchannel/72342371928702976/20040730/1837134.shtml

[Article] Author: Feng Jie Time: 2004-07-30 Source: NEW YORK Editor: Ark

[Article Guide] This article uses multi-threaded technology to realize the fast search of documents, very small code amount, high execution efficiency

[Text] 1 2 next page

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 two functions for two Win32

Handle FindFirstFile (lpctstr lpfilename, lpwin32_find_data lpfiledfiledata); Bool FindNextFile (Handle HfindFile, LPWIN32_FIND_DATA LPFINDFILEDATA); 2. Create a thread function First to define the parameter structure of the thread for file information:

Typedef struct tagthreadparam {cstring strpath; cstring strfilename;} threadparam m_param; definition of mutex due to threads to manipulate global variables:

CMutex M_MutexThreadCount, M_MutexThreadParam, M_MutexPath; If search is finished or search termination, 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;} 1 2 Next

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

New Post(0)