File: thread.cpp - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

01: 
02: #include <iostream>
03: #include "windows.h"
04: #include "process.h"
05: 
06: //#define TEST_CRT
07: #define TEST_WINAPI
08: 
09: #ifdef TEST_CRT
10:     #define TEST_FUNC_CREATE(c) (HANDLE)_beginthreadex (NULL, 0, (c), 0, CREATE_SUSPENDED, NULL)
11:     #define TEST_FUNC_DELETE(h) ({ if ((h)) { WaitForSingleObject ((h), INFINITE); CloseHandle ((h)); }})
12:     #define TEST_FUNC_RESUME(h) ({ if ((h)) ResumeThread ((h)); })
13:     #define TEST_RETURN         unsigned int __stdcall
14: #endif
15: 
16: #ifdef TEST_WINAPI
17:     #define TEST_FUNC_CREATE(c) CreateThread (NULL, 0, (c), 0, CREATE_SUSPENDED, NULL)
18:     #define TEST_FUNC_DELETE(h) ({ if ((h)) { WaitForSingleObject ((h), INFINITE); CloseHandle ((h)); }})
19:     #define TEST_FUNC_RESUME(h) ({ if ((h)) ResumeThread ((h)); })
20:     #define TEST_RETURN         DWORD WINAPI
21: #endif
22: 
23: struct  Ex
24: {
25: };
26: 
27: TEST_RETURN MyCallback (void*)
28: {
29:     try
30:     {
31:         throw Ex ();
32:     }
33:     catch (Ex&)
34:     {
35:     }
36: 
37:     return 0;
38: }
39: 
40: const int   count = 100;
41: const int   loop = 10;
42: 
43: int main()
44: {
45:     HANDLE  threads[count];
46:     int     i;
47:     int     j;
48: 
49:     for (i = 0; i++ < loop; )
50:     {
51:         std::cout << "Iteration #" << i << std::endl;
52: 
53:         std::cout << "    Create threads..." << std::endl;
54: 
55:         for (j = 0; j++ < count; )
56:             threads[j] = TEST_FUNC_CREATE (&MyCallback);
57: 
58:         Sleep (500);
59: 
60:         std::cout << "    Resume theads..." << std::endl;
61: 
62:         for (j = 0; j++ < count; )
63:             TEST_FUNC_RESUME (threads[j]);
64: 
65:         Sleep (500);
66: 
67:         std::cout << "    Delete threads..." << std::endl;
68: 
69:         for (j = 0; j++ < count; )
70:             TEST_FUNC_DELETE (threads[j]);
71: 
72:         Sleep (500);
73: 
74:         std::cout << "    Done." << std::endl;
75:     }
76: 
77:     return 0;
78: }