aboutsummaryrefslogtreecommitdiff
path: root/example/demo1.cc
blob: aa13c0c55068d1164e26d8696028ee6cacb326e2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Copyright (c) 2020 Johannes Stoelp */

#include "lib/executor.h"
#include "lib/thread.h"

#include <cstdio>
#include <memory>

struct TestThread : public nMatcha::Thread {
    TestThread(const char* name) : Thread(), mName(name) {}

    virtual void threadFn() override {
        printf("[%s] starting up TestThread -> yield()\n", mName);
        yield();
        printf("[%s] yield() -> finishing TestThreads\n", mName);
    }

  private:
    const char* mName;
};

int main() {
    puts("[main] start main thread");

    nMatcha::Executor e;
    e.spawn(std::make_unique<TestThread>("Thread1"));
    e.spawn(std::make_unique<TestThread>("Thread2"));
    e.spawn(std::make_unique<TestThread>("Thread3"));
    e.run();
    e.run();

    puts("[main] finish main thread");
    return 0;
}