testdb.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // testdb.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "testdb.h"
  5. #include <process.h>
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. // 唯一的应用程序对象
  12. CWinApp theApp;
  13. using namespace std;
  14. //DBPoolInterface *pInstance = NULL;
  15. volatile LONG nThreadCount = 0;
  16. //unsigned __stdcall ThreadWork( LPVOID lpInput )
  17. DWORD WINAPI ThreadWork( LPVOID lpInput )
  18. {
  19. DBPoolInterface *pInstance = (DBPoolInterface *)lpInput;
  20. DWORD dwThreadId = GetCurrentThreadId();
  21. CArray<CStringArray,CStringArray> Ary;
  22. pInstance->GetOrderInfo( &Ary , 5000);
  23. Sleep(10);
  24. //InterlockedIncrement(&nThreadCount);
  25. // printf("Thread Finish Count = %d\n", nThreadCount);
  26. //printf("log[%d]:\t %d\n",dwThreadId,nThreadCount);
  27. //
  28. for ( int i = 0; i < Ary.GetSize(); i++ )
  29. {
  30. CStringArray &strAry = Ary.ElementAt(i);
  31. for ( int n = 0; n < strAry.GetSize(); n++ )
  32. {
  33. printf("log[%d]: %s\n",dwThreadId, strAry.ElementAt(n));
  34. }
  35. }
  36. return 0;
  37. }
  38. int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
  39. {
  40. int nRetCode = 0;
  41. // 初始化 MFC 并在失败时显示错误
  42. if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
  43. {
  44. // TODO: 更改错误代码以符合您的需要
  45. _tprintf(_T("错误: MFC 初始化失败\n"));
  46. nRetCode = 1;
  47. }
  48. else
  49. {
  50. // TODO: 在此处为应用程序的行为编写代码。
  51. //////////////////////////////////////////////////////////////////////////
  52. // 测试用例;
  53. DBPoolInterface *pInstance = CreateDBPoolInterface();
  54. if ( pInstance == NULL )
  55. {
  56. printf("log:打开数据库连接池失败\n");
  57. return -1;
  58. }
  59. DWORD dwCount = pInstance->InitDBConnPool(_T("IT-PC\\SQLEXPRESS"),
  60. 0,
  61. _T("sa"),
  62. _T("ly1234"),
  63. _T("db"),
  64. 200,
  65. 200);
  66. if ( dwCount == 0 )
  67. {
  68. printf("log:创建连接对象0个,退出\n");
  69. return -1;
  70. }
  71. int ncount = 0;
  72. for ( int i = 0; i < 500; i++)
  73. {
  74. //Sleep(5);// 注释这个,会内存泄漏;
  75. unsigned int nThreadID = 0;
  76. HANDLE hThread = CreateThread(NULL, 0, ThreadWork, pInstance, 0, NULL);
  77. //HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, ThreadWork, pInstance, 0, &nThreadID);
  78. if (hThread)
  79. {
  80. ncount++;
  81. CString strNum = _T("");
  82. strNum.Format(_T("----创建线程成功: %d--------\n"), ncount);
  83. OutputDebugString(strNum);
  84. CloseHandle(hThread);
  85. //_endthreadex(hThread);
  86. }
  87. }
  88. system("pause");
  89. printf("\t\t %d \n\n\n",nThreadCount);
  90. pInstance->Release(); // 必须保证线程全部执行完成后才可以结束!
  91. system("pause");
  92. }
  93. //_CrtDumpMemoryLeaks();
  94. return nRetCode;
  95. }