aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/tutorial/thread1/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'externals/grill/flext/tutorial/thread1/main.cpp')
-rw-r--r--externals/grill/flext/tutorial/thread1/main.cpp79
1 files changed, 79 insertions, 0 deletions
diff --git a/externals/grill/flext/tutorial/thread1/main.cpp b/externals/grill/flext/tutorial/thread1/main.cpp
new file mode 100644
index 00000000..878cef76
--- /dev/null
+++ b/externals/grill/flext/tutorial/thread1/main.cpp
@@ -0,0 +1,79 @@
+/*
+flext tutorial - threads 1
+
+Copyright (c) 2002 Thomas Grill (xovo@gmx.net)
+For information on usage and redistribution, and for a DISCLAIMER OF ALL
+WARRANTIES, see the file, "license.txt," in this distribution.
+
+-------------------------------------------------------------------------
+
+This shows an example of a method running as a thread
+*/
+
+// define FLEXT_THREADS for thread usage. Flext must also have been compiled with that defined!
+#ifndef FLEXT_THREADS
+#define FLEXT_THREADS
+#endif
+
+#include <flext.h>
+
+#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 301)
+#error You need at least flext version 0.3.1
+#endif
+
+
+class thread1:
+ public flext_base
+{
+ FLEXT_HEADER(thread1,flext_base)
+
+public:
+ thread1();
+
+protected:
+ void m_start(); // method function
+
+private:
+ // define threaded callback for method m_start (with boolean argument)
+ // the same syntax as with FLEXT_CALLBACK is used here
+ FLEXT_THREAD(m_start);
+};
+
+FLEXT_NEW("thread1",thread1)
+
+
+
+thread1::thread1()
+{
+ AddInAnything();
+ AddOutInt();
+ SetupInOut(); // set up inlets and outlets
+
+ FLEXT_ADDBANG(0,m_start); // register method
+}
+
+void thread1::m_start()
+{
+ // Please note that this functions needs about 10 seconds to complete
+ // Without threads it would block the real-time system
+
+ // Okay, that functionality would be far more elegant with timers
+ // ... but hey, it's a demo!
+
+ for(int i = 0; i < 20 && !ShouldExit(); ++i) {
+ ToOutInt(0,i); // output loop count
+
+ // wait for half a second
+ for(int j = 0; j < 5 && !ShouldExit(); ++j) Sleep(0.1f);
+ // note: we shall not block a thread for a longer time.
+ // The system might want to destroy the object in the meantime and
+ // expects thread termination.
+ // Flext waits for 1 second by default, then it aborts the thread brutally
+ }
+
+ // output a final zero
+ ToOutInt(0,0);
+}
+
+
+