// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: ThreadSample1.jlc,v 1.25 2003/09/26 15:55:25 florian Exp $ */ #include #include #include //#include using namespace jakelib::lang; using namespace jakelib::io; #ifdef JAKELIB_THREADS class Synchronized : public Object, public Synchronizeable { public: Synchronized() {}; JAKELIB_DECLARE_CLASS(Synchronized); }; JAKELIB_IMPLEMENT_CLASS("Synchronized", Synchronized, Object); Synchronized* synchro; class T1 : public Thread { public: T1 (char c, jlong t) { this->t = t; this->c = c; } void run() { try { for (int idx = 0; idx < 5; idx++) { sleep(10); { JAKELIB_SYNCHRONIZED(synchro); printf("%s ", JAKELIB_LATIN1(Thread::currentThread()->toString())); for (int x = 0; x < 10; x++) { JAKELIB_SYNCHRONIZED(synchro); sleep(t); printf("%c%u", c, idx); fflush(stdout); } printf("\n"); } } } catch (Exception *ex) { System::out->println(ex); } } private: jlong t; char c; }; class T3 : public Thread, public Synchronizeable { public: T3() {} void run() { sleep(1000); JAKELIB_SYNCHRONIZED(this); for (int idx = 0; idx < 5; idx++) { //System::out->println("before lock"); //System::out->println("lock acquired"); for (int x = 0; x < 10; x++) { sleep(30); printf("."); fflush(stdout); } //System::out->println("notifyAll v"); //notifyAll(); //System::out->println("notifyAll n"); } } }; int main(int argc, char* argv[]) { initJakelib2(argc, argv); return 0; synchro = new Synchronized(); try { JAKELIB_MSG(`""` .. argv[0] .. `":"`); T1* t1 = new T1('A', 20); T1* t2 = new T1('B', 40); T1* t3 = new T1('C', 40); t2->start(); t1->start(); t3->start(); t2->join(); t1->join(); t3->join(); //T3* t3 = new T3(); //t3->start(); // { // JAKELIB_SYNCHRONIZED(t3); // for (int idx = 0; idx < 5; idx++) { // System::out->println("???"); // //t3->wait(); // System::out->println("!"); // } // } } catch (Exception *ex) { System::out->println(ex); } return 0; } #else int main(int argc, char* argv[]) { JAKELIB_INIT; jakelib::lang::System::out->println(`""` .. argv[0] .. `": Multi Threading not available.\n"`); return 0; } #endif