the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1// =================================================================================================================================
2// This sample is more of a show-(and test) case for HeapInspector's. It demonstrates:
3// 1) That HeapInspector is multithread safe.
4// 2) HeapInspector's ability to deal with allocations prior to Initialise (although those allocations will not be tracked).
5// 3) HeapInspector's ability to deal with API calls during static initialisation phase.
6//
7// In this sample, multiple threads are started that perform allocations for a set period of time. The
8// application will wait for those threads to finish. After the time is passed and the application calls Shutdown,
9// the client will disconnect.
10//
11// To switch between launching the threads during the static initalisation phase and launching the treads
12// in main, flip the INIT_IN_STATIC_PHASE define.
13//
14// =================================================================================================================================
15
16#include "IThread.h"
17#include <stdlib.h>
18
19void Wait(int a_MilliSeconds);
20
21#define INIT_IN_STATIC_PHASE 0
22const int g_NumThreads = 4;
23
24class MultiThreadedAllocator
25{
26public:
27 static void WorkerThread()
28 {
29 for (int i = 0; i != 1000; ++i)
30 {
31 void* mem1 = malloc(10);
32 Wait(10);
33 free(mem1);
34 Wait(10);
35 }
36 }
37
38 MultiThreadedAllocator()
39 {
40 for (int i = 0; i != g_NumThreads; ++i)
41 {
42 m_Threads[i] = CreateThread();
43 m_Threads[i]->Fork(WorkerThread);
44 }
45 }
46
47 ~MultiThreadedAllocator()
48 {
49 WaitForThreads();
50 for (int i = 0; i != g_NumThreads; ++i)
51 {
52 DestroyThread(m_Threads[i]);
53 }
54 }
55
56private:
57 void WaitForThreads()
58 {
59 for (int i = 0; i != g_NumThreads; ++i)
60 {
61 m_Threads[i]->Join();
62 }
63 }
64
65private:
66 IThread* m_Threads[g_NumThreads];
67};
68
69#if INIT_IN_STATIC_PHASE
70static MultiThreadedAllocator* g_Allocator = new MultiThreadedAllocator();
71#endif
72
73void RunHeapInspectorServer()
74{
75#if !INIT_IN_STATIC_PHASE
76 MultiThreadedAllocator* g_Allocator = new MultiThreadedAllocator();
77#endif
78
79 delete g_Allocator;
80}