// testdb.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "testdb.h" #include #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // 唯一的应用程序对象 CWinApp theApp; using namespace std; //DBPoolInterface *pInstance = NULL; volatile LONG nThreadCount = 0; //unsigned __stdcall ThreadWork( LPVOID lpInput ) DWORD WINAPI ThreadWork( LPVOID lpInput ) { DBPoolInterface *pInstance = (DBPoolInterface *)lpInput; DWORD dwThreadId = GetCurrentThreadId(); CArray Ary; pInstance->GetOrderInfo( &Ary , 5000); Sleep(10); //InterlockedIncrement(&nThreadCount); // printf("Thread Finish Count = %d\n", nThreadCount); //printf("log[%d]:\t %d\n",dwThreadId,nThreadCount); // for ( int i = 0; i < Ary.GetSize(); i++ ) { CStringArray &strAry = Ary.ElementAt(i); for ( int n = 0; n < strAry.GetSize(); n++ ) { printf("log[%d]: %s\n",dwThreadId, strAry.ElementAt(n)); } } return 0; } int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // 初始化 MFC 并在失败时显示错误 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: 更改错误代码以符合您的需要 _tprintf(_T("错误: MFC 初始化失败\n")); nRetCode = 1; } else { // TODO: 在此处为应用程序的行为编写代码。 ////////////////////////////////////////////////////////////////////////// // 测试用例; DBPoolInterface *pInstance = CreateDBPoolInterface(); if ( pInstance == NULL ) { printf("log:打开数据库连接池失败\n"); return -1; } DWORD dwCount = pInstance->InitDBConnPool(_T("IT-PC\\SQLEXPRESS"), 0, _T("sa"), _T("ly1234"), _T("db"), 200, 200); if ( dwCount == 0 ) { printf("log:创建连接对象0个,退出\n"); return -1; } int ncount = 0; for ( int i = 0; i < 500; i++) { //Sleep(5);// 注释这个,会内存泄漏; unsigned int nThreadID = 0; HANDLE hThread = CreateThread(NULL, 0, ThreadWork, pInstance, 0, NULL); //HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadWork, pInstance, 0, &nThreadID); if (hThread) { ncount++; CString strNum = _T(""); strNum.Format(_T("----创建线程成功: %d--------\n"), ncount); OutputDebugString(strNum); CloseHandle(hThread); //_endthreadex(hThread); } } system("pause"); printf("\t\t %d \n\n\n",nThreadCount); pInstance->Release(); // 必须保证线程全部执行完成后才可以结束! system("pause"); } //_CrtDumpMemoryLeaks(); return nRetCode; }