// -*- 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: ThreadTest.jlc,v 1.12 2003/09/26 15:55:26 florian Exp $ */ #include "Test.h" #include //#include //#include using namespace jakelib::util; using namespace jakelib::io; using namespace jakelib::lang; #ifdef JAKELIB_THREADS Semaphore sem; class A : public Thread { void run() { while(true) { //synchronized(sem); sleep(500); System::out->print(`"(A)"`); Thread::yield(); } } }; class B : public Thread { void run() { while(true) { //synchronized(sem); sleep(250); System::out->print(`"(B)"`); Thread::yield(); } } }; void threadTest() { start(`"jakelib::lang::Thread"`); try { System::out->print(`"Starting thread A: "`); A* a = new A(); System::out->println(a); a->start(); System::out->print(`"Starting thread B: "`); B* b = new B(); System::out->println(b); b->start(); System::out->println(`"Waiting for 4 seconds..."`); Thread::sleep(4000); System::out->println(`"\nStopping thread A, waiting for another 4 seconds..."`); Thread::sleep(4000); System::out->println(`"\nDeleting thread B, waiting for another 2 seconds..."`); delete b; Thread::sleep(2000); System::out->println(); } catch (Exception *e) { System::out->println(e); } } #else void threadTest() { start(`"jakelib::lang::Thread"`); jakelib::lang::System::out->println(`"Multi Threading not available."`); } #endif