zpp
Zephyr C++20 Framework
main.cpp
Go to the documentation of this file.
1 
8 #include <zephyr/kernel.h>
9 #include <zephyr/arch/cpu.h>
10 #include <zephyr/sys/arch_interface.h>
11 
12 #include <zpp.hpp>
13 #include <chrono>
14 
15 #define NUM_THREADS 3
16 #define TCOUNT 10
17 #define COUNT_LIMIT 12
18 
19 #define STACK_SIZE (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
20 
21 namespace {
22 
23 int count{};
24 
25 zpp::mutex count_mutex;
26 zpp::condition_variable count_threshold_cv;
27 
31 
32 } // anonimouse namespace
33 
34 void inc_count(int my_id) noexcept
35 {
36  for (int i = 0; i < TCOUNT; i++) {
37  {
38  zpp::lock_guard<zpp::mutex> lg(count_mutex);
39  count++;
40 
41  //
42  // Check the value of count and signal waiting thread when
43  // condition is reached. Note that this occurs while mutex is
44  // locked.
45  //
46 
47  if (count == COUNT_LIMIT) {
48  printk("%s: thread %d, count = %d Threshold reached.",
49  __func__, my_id, count);
50  count_threshold_cv.notify_one();
51  printk("Just sent signal.\n");
52  }
53 
54  printk("%s: thread %d, count = %d, unlocking mutex\n",
55  __func__, my_id, count);
56  }
57 
58  // Sleep so threads can alternate on mutex lock
59  zpp::this_thread::sleep_for(std::chrono::milliseconds(500));
60  }
61 }
62 
63 void watch_count(int my_id) noexcept
64 {
65  printk("Starting %s: thread %d\n", __func__, my_id);
66 
67  zpp::unique_lock lk(count_mutex);
68 
69  while (count < COUNT_LIMIT) {
70  printk("%s: thread %d Count= %d. Going into wait...\n",
71  __func__, my_id, count);
72 
73  count_threshold_cv.wait(lk);
74 
75  printk("%s: thread %d Condition signal received. Count= %d\n",
76  __func__, my_id, count);
77  }
78 
79  printk("%s: thread %d Updating the value of count...\n",
80  __func__, my_id);
81  count += 125;
82  printk("%s: thread %d count now = %d.\n", __func__, my_id, count);
83  printk("%s: thread %d Unlocking mutex.\n", __func__, my_id);
84 }
85 
86 int main(void)
87 {
88  const zpp::thread_attr attrs(
92  );
93 
94  t[0] = zpp::thread(tcb[0], tstack(0), attrs, watch_count, 1);
95 
96  t[1] = zpp::thread(tcb[1], tstack(1), attrs, inc_count, 2);
97  t[2] = zpp::thread(tcb[2], tstack(2), attrs, inc_count, 3);
98 
99  // Wait for all threads to complete
100  for (int i = 0; i < NUM_THREADS; i++) {
101  t[i].join();
102  }
103 
104  printk("Main(): Waited and joined with %d threads. Final value of count = %d. Done.\n",
105  NUM_THREADS, count);
106 
107  return 0;
108 }
A condition variable class.
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
zpp::unique_lock using zpp::mutex as a lock.
Definition: unique_lock.hpp:24
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
int main(int argc, char *argv[])
Definition: main.cpp:26
#define TCOUNT
Definition: main.cpp:16
#define COUNT_LIMIT
Definition: main.cpp:17
void inc_count(int my_id) noexcept
Definition: main.cpp:34
#define STACK_SIZE
Definition: main.cpp:19
#define NUM_THREADS
Definition: main.cpp:15
void watch_count(int my_id) noexcept
Definition: main.cpp:63
#define ZPP_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)