Skip to content

Commit

Permalink
Created CoreThread example
Browse files Browse the repository at this point in the history
  • Loading branch information
gtremper committed Jul 31, 2014
1 parent 586d098 commit 9f1881b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 8 deletions.
6 changes: 5 additions & 1 deletion examples/CMakeLists.txt
@@ -1,3 +1,7 @@
add_executable(ContextExample ContextExample.cpp)
target_link_libraries(ContextExample Autowiring)
set_property(TARGET ContextExample PROPERTY FOLDER "Examples")
set_property(TARGET ContextExample PROPERTY FOLDER "Examples")

add_executable(ThreadExample ThreadExample.cpp)
target_link_libraries(ThreadExample Autowiring)
set_property(TARGET ThreadExample PROPERTY FOLDER "Examples")
15 changes: 8 additions & 7 deletions examples/ContextExample.cpp
Expand Up @@ -22,10 +22,9 @@ int main() {
//// GlobalCoreContext ////
///////////////////////////
{
// The CoreContext is the basic unit of organization. They are organized
// in a tree structure, the root of which is the GlobalCoreContext. Each
// thread keeps track of its current context, which defaults to
// GlobalCoreContext
// The CoreContext is the basic unit of organization in Autowriring. They are organized
// in a tree structure, the root of which is the GlobalCoreContext. Each thread
// keeps track of its current context, which defaults to GlobalCoreContext

// This creates a shared_ptr to the global context
AutoGlobalContext global;
Expand All @@ -46,7 +45,7 @@ int main() {
// A helper type 'AutoCreateContext' will call Create on the current context
// automatically

AutoGlobalContext global; //same as AutoCurrentContext
AutoGlobalContext global; //same as AutoCurrentContext here

// Create's a chile of the current context
AutoCreateContext childCtxt; // Same as childCtxt = AutoCurrentContext()->Create<void>();
Expand Down Expand Up @@ -83,6 +82,8 @@ int main() {
// the same instance
AutoRequired<Foo> foo2;
check("foo2 is the same instance as foo", foo==foo2);
std::cout << "foo2 value of 'x': " << foo2->x << std::endl;

} // 'ctxt' and all members a destroyed when the context goes out of scope

//////////////////////
Expand Down Expand Up @@ -112,11 +113,11 @@ int main() {
// Bar has an Autowrired<Foo> classmember
AutoRequired<Bar> bar;

check("Foo member of bar isn't satisfied", !bar->foo);
check("Foo member of bar is satisfied before AutoRequired", bar->foo);

// If we inject Foo into the context, the Autowired<Foo> member of Bar will be satisfied
AutoRequired<Foo>();
check("Foo member of bar now is satisfied", bar->foo);
check("Foo member of bar is satisfied after AutoRequired", bar->foo);
}
}
}
65 changes: 65 additions & 0 deletions examples/ThreadExample.cpp
@@ -0,0 +1,65 @@
// Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved.
#include <autowiring/Autowired.h>
#include <autowiring/CoreThread.h>
#include <iostream>
#include <memory>
#include <thread>
#include <chrono>

class MyBasicThread:
public BasicThread
{
public:
virtual void Run() override {
for (auto x : {0, 1, 2 ,3}) {
std::cout << "MyBasicThread: " << x << "\n";
}
}
};

class MyCoreThread:
public CoreThread
{
public:
void AddToQueue(int x) {
*this += [x] {
std::cout << "MyCoreThread: " << x << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(500));
};
}
};

int main(){

// The 2 main thread classes in Autowiring are the BasicThread and CoreThread.
// Classes that inherit from these types will have thread capabilities
// Both start when their enclosing context is 'initiated'. Threads injected
// after the context is initiated will start immediatly

AutoRequired<MyBasicThread> myBasic;

AutoCurrentContext ctxt;
ctxt->Initiate(); // myBasic->Run() starts now in its own thread

std::this_thread::sleep_for(std::chrono::milliseconds(250));

std::cout << "injecting a CoreThread" << std::endl;

// Types inheriting from CoreThread implement a dispatch queue in their 'run()'
// function. Lambdas can be appended with operator+=

AutoRequired<MyCoreThread> myCore;
myCore->AddToQueue(42);
myCore->AddToQueue(1337);

*myCore += []{
std::cout << "This gets run after '1337'" << std::endl;
};

// This should be run before 'myCore' is finished
std::cout << "This thread is faster\n";

// This will wait for all outstanding threads to finish before terminating the context
ctxt->SignalShutdown(true);
}

0 comments on commit 9f1881b

Please sign in to comment.