安全的使用CreateThread()
来源:互联网 发布:js百度地图api 编辑:程序博客网 时间:2024/05/28 01:34
/*uses multiple threads to search the files
".c" in the current directory for the string given
on th command line;
this example avoids most C run-time functions,so
that it can use the single-thread Clibraries,
it is necessary to use a critical section to divvy
up output to the screen or the various threads end up
with their output intermingled .Normally the multithreaded
C run-time does this automatically if you use printf*/
#include<Windows.h>
#include "MtVerify.h"
DWORD WINAPI SearchProc(void *arg);
BOOL GetLine(HANDLE hFile, LPSTR buf, DWORD size);
#define MAX_THREADS 3
HANDLE hThreadLimitSemaphore;
HANDLE hConsoleOut;
CRITICAL_SECTION ScreenCritica;
char szSearchFor[1024];
int main(int argc, char *argv[])
{
WIN32_FIND_DATA *lpFindData;
HANDLE hFindFile;
HANDLE hThread;
DWORD dummy;
int i;
hConsoleOut = GetSetHandle(STD_OUTPUT_HANDLE);
if (argc != 2)
{
char errbuf[512];
wsprintf(errbuf, "Usage:%s<search-string>\n", argv[0]);
WriteFile(hConsoleOut, errbuf, strlen(errbuf), &dummy, FALSE);
return EXIT_FAILURE;
}
/*Put search string where everyone can see it */
strcpy(szSearchFor, argv[1]);
/* Allocate a find buffer to be handed to the first thread*/
lpFindData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WIN32_FIND_DATA));
/*Semaphore prevents too many threads from running*/
MTVERIFY(hThreadLimitSemaphore = CreateSemaphore(NULL, MAX_THREADS, MAX_THREADS, NULL));
InitializeCriticalSection(&ScreenCritical);
hFindFile = FindFirstFile("*.c", lpFindData);
if (hFindFile == INVALID_HANDLE_VALUE)
return EXIT_FAILURE;
do{
WaitForSingleObject(hThreadLimitSemaphore, INFINITE);
MTVERIFY(hThread = CreateThread(NULL, 0, SearchProc, lpFindData, 0, &dummy));
MTVERIFY(CloseHandle(hThread));
lpFindData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
sizeof(WIN32_FIND_DATA));
} while (FindNextFile(hFindFile, lpFindData));
FindClose(hFindFile);
hFindFile = INVALID_HANDLE_VALUE;
for (i = 0; i < MAX_THREADS; i++)
WaitForSingleObject(hThreadLimitSemaphore, INFINITE);
MTVERIFY(CloseHandle(hThreadLimitSemaphore));
return EXIT_SUCCESS;
}
DWORD WINAPI SearchProc(void *arg)
{
WIN32_FIND_DATA *lpFindData = (WIN32_FIND_DATA*)arg;
char buf[1024];
HANDLE hFile;
DWORD dummy;
hFile = CreateFile(lpFindData->cFileName, GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (!hFile)
return 1;
while (GetLine(hFile, buf, sizeof(buf)))
{
if (strstr(buf, szSearchFor))
{
EnterCriticalSection(&ScreenCritical);
WriteFile(hConsoleOut, strlen(lpFindData->cFileName), &dummy, FALSE);
WriteFile(hConsoleOut, ":", 2, &dummy, FALSE);
WriteFile(hConsoleOut, buf, strlen(buf), &dummy, FALSE);
WriteFile(hConsoleOut, "\r\n", 2, &dummy, FALSE);
LeaveCriticalSection(&ScreenCritical);
}
}
CloseHandle(hFile);
HeapFree(GetProcessHeap(), 0, lpFindData);
MTVERIFY(ReleaseSemaphore(hThreadLimitSemaphore, 1, NULL));
}
BOOL GetLine(HANDLE hFile, LPSTR buf, DWORD size)
{
DWORD total = 0;
DWORD numread;
int state = 0;//0 = looking for non-newline
// 1 = stop after first newline
for (;;)
{
if (total == size - 1)
{
buf[size - 1] = '\0';
return TRUE;
}
if (!ReadFile(hFile, buf + total, 1, &numread, 0) ||
numread == 0)
{
buf[total] = '\0';
return total != 0;
}
if (buf[total] == 'r' || buf[total] == '\n')
{
if (state == 0)
continue;
buf[total] = '\0';
return TRUE;
}
state = 1;
total++;
}
}
- 安全的使用CreateThread()
- createThread函数的使用
- 使用CRT函数和CreateThread的注意事项
- CreateThread和_beginthread的区别及使用
- CreateThread函数设置安全权限
- CreateThread的用法及在Qt等GUI开发中使用CreateThread的一些技巧
- CreateThread函数使用
- 不要随便使用CreateThread
- [转载] CreateThread()使用小结
- 使用CreateThread创建线程
- CreateThread()使用实例
- 如何避免使用CreateThread函数导致的内存泄露
- 如何避免使用CreateThread函数导致的内存泄露
- 使用CreateThread函数和_beginThreadex函数的注意事项
- CreateThread、_beginthreadex、AfxBeginThread的区别和正确使用
- 如何避免使用CreateThread函数导致的内存泄露
- 多线程CreateThread函数的使用(六个参数介绍)
- CreateThread的应用
- 1015. Reversible Primes (20)
- Android——开发框架的使用说明
- 29个你必须知道的Linux命令
- Javascript的匿名函数与自执行
- 安卓工程的目录结构
- 安全的使用CreateThread()
- Microsoft Office PPT 编程(VBA)学习 (不定时更新,正在学习 ing)
- ORACLE日期时间函数大全
- io阻塞与io非阻塞
- 欢迎使用CSDN-markdown编辑器
- 临界区,互斥量,信号量,事件的区别
- 51单片机学习笔记【九】——红外通信实验
- hdu2222(看一些单词哪些在模式串中出现过)
- Cocos2d-x :什么是锚点?(简单点说:锚点就是图片的原点)