|
1 |
| -//===-- main.cpp ------------------------------------------------*- C++ -*-===// |
2 |
| -// |
3 |
| -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 |
| -// See https://llvm.org/LICENSE.txt for license information. |
5 |
| -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 |
| -// |
7 |
| -//===----------------------------------------------------------------------===// |
8 |
| - |
9 |
| -// C includes |
10 | 1 | #include <stdio.h>
|
11 |
| -#include <stdint.h> |
12 |
| -#include <stdlib.h> |
13 |
| - |
14 |
| -// C++ includes |
15 |
| -#include <chrono> |
16 |
| -#include <mutex> |
17 |
| -#include <random> |
18 |
| -#include <thread> |
19 |
| - |
20 |
| -std::thread g_thread_1; |
21 |
| -std::thread g_thread_2; |
22 |
| -std::thread g_thread_3; |
23 |
| -std::mutex g_mask_mutex; |
24 |
| - |
25 |
| -enum MaskAction { |
26 |
| - eGet, |
27 |
| - eAssign, |
28 |
| - eClearBits |
29 |
| -}; |
30 |
| - |
31 |
| -uint32_t mask_access (MaskAction action, uint32_t mask = 0); |
32 |
| - |
33 |
| -uint32_t |
34 |
| -mask_access (MaskAction action, uint32_t mask) |
35 |
| -{ |
36 |
| - static uint32_t g_mask = 0; |
37 |
| - |
38 |
| - std::lock_guard<std::mutex> lock(g_mask_mutex); |
39 |
| - switch (action) |
40 |
| - { |
41 |
| - case eGet: |
42 |
| - break; |
43 |
| - |
44 |
| - case eAssign: |
45 |
| - g_mask |= mask; |
46 |
| - break; |
47 |
| - |
48 |
| - case eClearBits: |
49 |
| - g_mask &= ~mask; |
50 |
| - break; |
51 |
| - } |
52 |
| - return g_mask; |
53 |
| -} |
54 |
| - |
55 |
| -void * |
56 |
| -thread_func (void *arg) |
57 |
| -{ |
58 |
| - uint32_t thread_index = *((uint32_t *)arg); |
59 |
| - uint32_t thread_mask = (1u << (thread_index)); |
60 |
| - printf ("%s (thread index = %u) startng...\n", __FUNCTION__, thread_index); |
61 |
| - |
62 |
| - std::default_random_engine generator; |
63 |
| - std::uniform_int_distribution<int> distribution(0, 3000000); |
64 |
| - |
65 |
| - while (mask_access(eGet) & thread_mask) |
66 |
| - { |
67 |
| - // random micro second sleep from zero to 3 seconds |
68 |
| - int usec = distribution(generator); |
69 |
| - |
70 |
| - printf ("%s (thread = %u) doing a usleep (%d)...\n", __FUNCTION__, thread_index, usec); |
71 |
| - std::chrono::microseconds duration(usec); |
72 |
| - std::this_thread::sleep_for(duration); |
73 |
| - printf ("%s (thread = %u) after usleep ...\n", __FUNCTION__, thread_index); // Set break point at this line. |
74 |
| - } |
75 |
| - printf ("%s (thread index = %u) exiting...\n", __FUNCTION__, thread_index); |
76 |
| - return NULL; |
77 |
| -} |
78 |
| - |
79 |
| - |
80 |
| -int main (int argc, char const *argv[]) |
81 |
| -{ |
82 |
| - int err; |
83 |
| - void *thread_result = NULL; |
84 |
| - uint32_t thread_index_1 = 1; |
85 |
| - uint32_t thread_index_2 = 2; |
86 |
| - uint32_t thread_index_3 = 3; |
87 |
| - uint32_t thread_mask_1 = (1u << thread_index_1); |
88 |
| - uint32_t thread_mask_2 = (1u << thread_index_2); |
89 |
| - uint32_t thread_mask_3 = (1u << thread_index_3); |
90 |
| - |
91 |
| - // Make a mask that will keep all threads alive |
92 |
| - mask_access (eAssign, thread_mask_1 | thread_mask_2 | thread_mask_3); // And that line. |
93 |
| - |
94 |
| - // Create 3 threads |
95 |
| - g_thread_1 = std::thread(thread_func, (void*)&thread_index_1); |
96 |
| - g_thread_2 = std::thread(thread_func, (void*)&thread_index_2); |
97 |
| - g_thread_3 = std::thread(thread_func, (void*)&thread_index_3); |
98 |
| - |
99 |
| - char line[64]; |
100 |
| - while (mask_access(eGet) != 0) |
101 |
| - { |
102 |
| - printf ("Enter thread index to kill or ENTER for all:\n"); |
103 |
| - fflush (stdout); |
104 |
| - // Kill threads by index, or ENTER for all threads |
105 |
| - |
106 |
| - if (fgets (line, sizeof(line), stdin)) |
107 |
| - { |
108 |
| - if (line[0] == '\n' || line[0] == '\r' || line[0] == '\0') |
109 |
| - { |
110 |
| - printf ("Exiting all threads...\n"); |
111 |
| - break; |
112 |
| - } |
113 |
| - int32_t index = strtoul (line, NULL, 0); |
114 |
| - switch (index) |
115 |
| - { |
116 |
| - case 1: mask_access (eClearBits, thread_mask_1); break; |
117 |
| - case 2: mask_access (eClearBits, thread_mask_2); break; |
118 |
| - case 3: mask_access (eClearBits, thread_mask_3); break; |
119 |
| - } |
120 |
| - continue; |
121 |
| - } |
122 |
| - |
123 |
| - break; |
124 |
| - } |
125 |
| - |
126 |
| - // Clear all thread bits to they all exit |
127 |
| - mask_access (eClearBits, UINT32_MAX); |
128 |
| - |
129 |
| - // Join all of our threads |
130 |
| - g_thread_1.join(); |
131 |
| - g_thread_2.join(); |
132 |
| - g_thread_3.join(); |
133 | 2 |
|
134 |
| - return 0; |
| 3 | +int main() { |
| 4 | + printf("Hello World\n"); |
135 | 5 | }
|
0 commit comments