zpp
Zephyr C++20 Framework
main.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019 Erwin Rol <erwin@erwinrol.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 
7 #include <zpp.hpp>
8 
9 #include <chrono>
10 
11 //
12 // using a anonymous namespace for file local variables and functions
13 //
14 namespace {
15 
16 //
17 // Define the Thread Control Block for the thread, using a stack of 1024 bytes
18 //
19 ZPP_THREAD_STACK_DEFINE(my_thread_tstack, 1024);
20 zpp::thread_data my_thread_tcb;
21 
22 zpp::heap<128> my_heap;
23 
24 } // namespace
25 
26 int main(int argc, char *argv[])
27 {
28  //
29  // use zpp and std::chrono namespaces in the function
30  //
31  using namespace zpp;
32  using namespace std::chrono;
33 
34  mutex print_lock;
35 
36  //
37  // Create thread attributes used for thread creation
38  //
39  const thread_attr my_thread_attr(
43  );
44 
45  //
46  // Create the first thread, using a lambda passing a const char* as
47  // argument that is used as "template" for the print out.
48  //
49  // The lock used for making sure the prints don't get messed up
50  // is captured by reference
51  //
52  const char* string_arg = "Hello World from thread tid={}\n";
53 
54  auto my_thread = thread(
55  my_thread_tcb, my_thread_tstack(), my_thread_attr, &my_heap,
56  [&print_lock](const char* t) noexcept
57  {
58  while (true) {
59  //
60  // Use a lock_guard in a scope to automatically
61  // do the unlocking when leaving the scope
62  //
63  {
64  lock_guard lg(print_lock);
65 
66  //
67  // print the thread ID of this thread
68  //
70  }
71 
72  //
73  // let this thread sleep for 500 ms without
74  // holding the lock
75  //
77  }
78  }, string_arg);
79 
80  //
81  // Loop forever, because ending main would not only end the
82  // program, my_thread would also go out of scope aborting the thread.
83  //
84  while (true) {
85  // Use a lock_guard in a scope to automatically
86  // do the unlocking when leaving the scope
87  //
88  {
89  lock_guard lg(print_lock);
90 
91  //
92  // print the thread ID of the main thread
93  //
94  print("Hello World from main tid={}\n",
96  }
97 
98  //
99  // let main thread sleep for 1 s without holding the lock
100  //
102  }
103 
104  return 0;
105 }
heap class
Definition: heap.hpp:159
lock_guard using zpp::mutex as a lock.
Definition: lock_guard.hpp:23
A recursive mutex class.
Definition: mutex.hpp:150
Thread creation attributes.
Definition: thread_attr.hpp:35
thread_data holds the stack and thread control block memory
Definition: thread_data.hpp:19
static constexpr thread_prio preempt(int prio) noexcept
Create a preemptive priority value.
The class thread repecents a single Zephyr thread.
Definition: thread.hpp:147
auto get_id() noexcept
Get the thread ID of the current thread.
Definition: thread.hpp:42
auto sleep_for(const std::chrono::duration< T_Rep, T_Period > &sleep_duration)
Suspend the current thread for a specified time duration.
Definition: thread.hpp:77
void print(const char *fmt, T_Args &&... args) noexcept
simple typesafe print function
Definition: fmt.hpp:157
int main(int argc, char *argv[])
Definition: main.cpp:26
#define ZPP_THREAD_STACK_DEFINE(sym, size)