Skip to content

Commit

Permalink
Made simple example simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
shuhaowu committed Aug 16, 2024
1 parent 24c3e1c commit 084939b
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions examples/simple_example/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,33 @@ using cactus_rt::App;
using cactus_rt::CyclicThread;

/**
* This is a no-op thread that does nothing at 1 kHz.
* This is a thread that loops at 1 kHz.
*/
class ExampleRTThread : public CyclicThread {
int64_t loop_counter_ = 0;
static cactus_rt::CyclicThreadConfig MakeConfig() {
cactus_rt::CyclicThreadConfig config;
config.period_ns = 1'000'000;
config.cpu_affinity = std::vector<size_t>{2};
config.SetFifoScheduler(80);
return config;
}

public:
ExampleRTThread(const char* name, cactus_rt::CyclicThreadConfig config) : CyclicThread(name, config) {}

int64_t GetLoopCounter() const {
return loop_counter_;
}
ExampleRTThread() : CyclicThread("ExampleRTThread", MakeConfig()) {}

protected:
bool Loop(int64_t /*now*/) noexcept final {
loop_counter_++;
if (loop_counter_ % 1000 == 0) {
LOG_INFO(Logger(), "Loop {}", loop_counter_);
}
bool Loop(int64_t elapsed_ns) noexcept final {
// Write your code here that requires 1 kHz.

LOG_INFO_LIMIT(std::chrono::seconds(1), Logger(), "Looping for {}", std::chrono::nanoseconds(elapsed_ns));
return false;
}
};

int main() {
cactus_rt::CyclicThreadConfig config;
config.period_ns = 1'000'000;
config.cpu_affinity = std::vector<size_t>{2};
config.SetFifoScheduler(80);

auto thread = std::make_shared<ExampleRTThread>("ExampleRTThread", config);
auto thread = std::make_shared<ExampleRTThread>();

App app;
app.StartTraceSession("build/data.perfetto");
app.RegisterThread(thread);

constexpr unsigned int time = 5;
Expand All @@ -49,6 +44,7 @@ int main() {
app.RequestStop();
app.Join();

std::cout << "Number of loops executed: " << thread->GetLoopCounter() << "\n";
std::cout << "Done\n";

return 0;
}

0 comments on commit 084939b

Please sign in to comment.