zpp
Zephyr C++20 Framework
main.cpp File Reference
#include <zpp.hpp>
#include <chrono>
Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file main.cpp.

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 }
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
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

References zpp::this_thread::get_id(), zpp::no, zpp::thread_prio::preempt(), zpp::print(), and zpp::this_thread::sleep_for().