aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/flext/tutorial/thread2/main.cpp
diff options
context:
space:
mode:
authorThomas Grill <xovo@users.sourceforge.net>2002-10-22 23:07:10 +0000
committerThomas Grill <xovo@users.sourceforge.net>2002-10-22 23:07:10 +0000
commitd62e56f4df9594f72ce501f5e19c974fd18e7295 (patch)
tree635d4af7a7c2425098e60ca277086ec436b617f7 /externals/grill/flext/tutorial/thread2/main.cpp
parentc6f373c281ecb5cd1f4aa7a070e15cc61ab8793c (diff)
This commit was generated by cvs2svn to compensate for changes in r186,
which included commits to RCS files with non-trunk default branches. svn path=/trunk/; revision=187
Diffstat (limited to 'externals/grill/flext/tutorial/thread2/main.cpp')
-rw-r--r--externals/grill/flext/tutorial/thread2/main.cpp132
1 files changed, 132 insertions, 0 deletions
diff --git a/externals/grill/flext/tutorial/thread2/main.cpp b/externals/grill/flext/tutorial/thread2/main.cpp
new file mode 100644
index 00000000..ce2a170e
--- /dev/null
+++ b/externals/grill/flext/tutorial/thread2/main.cpp
@@ -0,0 +1,132 @@
+/*
+flext tutorial - threads 2
+
+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 multiple threads and syncing with a thread conditional
+*/
+
+// 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 thread2:
+ public flext_base
+{
+ FLEXT_HEADER(thread2,flext_base)
+
+public:
+ thread2(int del);
+
+protected:
+ void m_start(int st);
+ void m_stop();
+ void m_text();
+
+ void m_textout();
+
+private:
+ FLEXT_THREAD_I(m_start); // define threaded callback for method m_start
+ FLEXT_CALLBACK(m_stop); // normal callback for m_stop
+ FLEXT_CALLBACK(m_text); // turn on console output
+
+ FLEXT_THREAD(m_textout); // text output
+
+ float delay;
+ volatile int count;
+
+ // caution: CodeWarrior seems to ignore volatile modifier!!
+ volatile bool stopit,running,blipping; // flags for running and stopping
+
+ // thread conditional for stop signal
+ ThrCond cond;
+};
+
+FLEXT_NEW_1("thread2",thread2,int)
+
+
+
+thread2::thread2(int del):
+ delay(del/1000.f),
+ stopit(false),
+ running(false),blipping(false)
+{
+ AddInAnything();
+ AddOutInt(2);
+ SetupInOut(); // set up inlets and outlets
+
+ FLEXT_ADDMETHOD(0,m_start); // register start for integer numbers (floats in PD)
+ FLEXT_ADDMETHOD_(0,"text",m_text); // register m_text method for "text" tag
+ FLEXT_ADDMETHOD_(0,"stop",m_stop); // register m_text method for "stop" tag
+}
+
+void thread2::m_start(int st)
+{
+ // if already running, just set back the counter
+ if(running) { count = st; return; }
+
+ running = true;
+
+ // loop until either the system exit flag or the "stopit" flag is set
+ for(count = st; !ShouldExit() && !stopit; ++count)
+ {
+ Sleep(delay);
+ ToOutInt(0,count); // output loop count
+ }
+
+ cond.Lock(); // lock conditional
+ running = false; // change state flag
+ cond.Signal(); // signal changed flag to watiting "stop" method
+ cond.Unlock(); // unlock conditional
+}
+
+void thread2::m_stop()
+{
+ cond.Lock(); // lock conditional
+ stopit = true; // set termination flag
+
+ while(*(&running) || *(&blipping)) // workaround for CodeWarrior!
+ {
+ cond.Wait(); // wait for signal by running threads
+ }
+
+ // --- Here, the threads should have stopped ---
+
+ stopit = false; // reset flag
+ cond.Unlock(); // unlock conditional
+}
+
+
+void thread2::m_text()
+{
+ FLEXT_CALLMETHOD(m_textout);
+}
+
+void thread2::m_textout()
+{
+ if(blipping) return;
+ blipping = true;
+
+ while(!ShouldExit() && !stopit) {
+ post("%i",count);
+ Sleep(1.f);
+ }
+
+ cond.Lock(); // lock conditional
+ blipping = false; // change state flag
+ cond.Signal(); // signal changed flag to watiting "stop" method
+ cond.Unlock(); // unlock conditional
+}
+