Skip to content.

Sections
Personal tools
You are here: Home » ダウンロード » ThreadJack用 » event

Document Actions
#include <iostream>
#include <threadjack.h>
#include <tjutil.h>
 

class Event {
public:
    int number;
    int a,b;
};
 

class Source : public TJThread,
               public TJSafeEventSource<Event> {
public:
    Source(int number) : myNumber(number) { }

    virtual void run() {
        int cnt = 0;
        for (int i=0; i<5; i++) {
            Event ev;
            ev.number = myNumber;
            ev.a      = cnt++;
            ev.b      = cnt++;
            cout << "Source" << myNumber << ": Calling fireEvent()" << endl;
           fireEvent(ev);
        }
    }
private:
    int myNumber;
};

//class Listener : public TJEventListener<Event> {
class Listener : public TJSafeEventListener<Event> {
public:
    virtual void eventHappened(const Event& ev) {
        cout << "  Listener: Source" << ev.number << ": ";
        cout << "ev.a=" << ev.a << ", ev.b=" << ev.b << endl;

        cout << "  Listener: start long-run loop" << endl;
        volatile int cnt = 0;
        for (int i=0; i<100; i++)
            for (int j=0; j<10000; j++) cnt++;
        cout << "  Listener: finish " << cnt << " loop" << endl;
    }
};
 

int main() {
    Listener l;
    Source* s1 = new Source(1);
    Source* s2 = new Source(2);

    s1->addEventListener(&l);
    s2->addEventListener(&l);

    s1->start();
    s2->start();

    s1->join();
    s2->join();

    return 0;
}