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(
40
thread_prio::preempt
(0),
41
thread_inherit_perms::no
,
42
thread_suspend::no
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
//
69
print
(t,
this_thread::get_id
());
70
}
71
72
//
73
// let this thread sleep for 500 ms without
74
// holding the lock
75
//
76
this_thread::sleep_for
(500ms);
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"
,
95
this_thread::get_id
());
96
}
97
98
//
99
// let main thread sleep for 1 s without holding the lock
100
//
101
this_thread::sleep_for
(1s);
102
}
103
104
return
0;
105
}
zpp::heap
heap class
Definition:
heap.hpp:159
zpp::lock_guard
lock_guard using zpp::mutex as a lock.
Definition:
lock_guard.hpp:23
zpp::mutex
A recursive mutex class.
Definition:
mutex.hpp:150
zpp::thread_attr
Thread creation attributes.
Definition:
thread_attr.hpp:35
zpp::thread_data
thread_data holds the stack and thread control block memory
Definition:
thread_data.hpp:19
zpp::thread_prio::preempt
static constexpr thread_prio preempt(int prio) noexcept
Create a preemptive priority value.
Definition:
thread_prio.hpp:186
zpp::thread
The class thread repecents a single Zephyr thread.
Definition:
thread.hpp:147
zpp::this_thread::get_id
auto get_id() noexcept
Get the thread ID of the current thread.
Definition:
thread.hpp:42
zpp::this_thread::sleep_for
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
zpp
Definition:
atomic_bitset.hpp:15
zpp::print
void print(const char *fmt, T_Args &&... args) noexcept
simple typesafe print function
Definition:
fmt.hpp:157
zpp::thread_suspend::no
@ no
zpp::thread_inherit_perms::no
@ no
main
int main(int argc, char *argv[])
Definition:
main.cpp:26
ZPP_THREAD_STACK_DEFINE
#define ZPP_THREAD_STACK_DEFINE(sym, size)
Definition:
thread_stack.hpp:49
zpp.hpp
samples
hello_world
src
main.cpp
Generated by
1.9.1