zpp
Zephyr C++20 Framework
main.cpp File Reference
#include <zephyr/kernel.h>
#include <zephyr/arch/cpu.h>
#include <zephyr/sys/arch_interface.h>
#include <zpp.hpp>
#include <chrono>
Include dependency graph for main.cpp:

Go to the source code of this file.

Macros

#define NUM_THREADS   20
 
#define STACK_SIZE   (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)
 

Functions

int main (void)
 

Macro Definition Documentation

◆ NUM_THREADS

#define NUM_THREADS   20

Definition at line 14 of file main.cpp.

◆ STACK_SIZE

#define STACK_SIZE   (1024 + CONFIG_TEST_EXTRA_STACK_SIZE)

Definition at line 15 of file main.cpp.

Function Documentation

◆ main()

int main ( void  )

Definition at line 56 of file main.cpp.

57 {
58  const zpp::thread_attr attrs(
62  );
63 
64  for (int i = 0; i < NUM_THREADS; i++) {
65  t[i] = zpp::thread(tcb[i], tstack(i), attrs, &worker_thread, i);
66  }
67 
68  //zpp::this_thread::sleep_for(std::chrono::seconds(1));
69 
70  printk("[thread %s] all threads started\n", __func__);
71 
72  {
73  zpp::lock_guard lg(m);
74 
75  //
76  // are the other threads still busy?
77  //
78  while (done < NUM_THREADS) {
79  printk("[thread %s] done is %d which is < %d so waiting on cond\n",
80  __func__, done, (int)NUM_THREADS);
81 
82  // block this thread until another thread signals cond. While
83  // blocked, the mutex is released, then re-acquired before this
84  // thread is woken up and the call returns.
85  auto res = cv.wait(m);
86  __ASSERT_NO_MSG(res == true);
87 
88  printk("[thread %s] wake - cond was signalled.\n", __func__);
89 
90  // we go around the loop with the lock held
91  }
92 
93  } // zpp::lock_guard scope
94 
95  for (int i = 0; i < NUM_THREADS; i++) {
96  auto res = t[i].join();
97  __ASSERT_NO_MSG(res == true);
98  }
99 
100  printk("[thread %s] done == %d so everyone is done\n",
101  __func__, (int)NUM_THREADS);
102 
103  return 0;
104 }
lock_guard using zpp::mutex as a lock.
Definition: lock_guard.hpp:23
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
#define NUM_THREADS
Definition: main.cpp:14

References zpp::no, NUM_THREADS, and zpp::thread_prio::preempt().