// // thrtest.cc // based on: messtest.cc // // Copyright (C) 1996 Limit Point Systems, Inc. // // Author: Curtis Janssen // Maintainer: LPS // // This file is part of the SC Toolkit. // // The SC Toolkit is free software; you can redistribute it and/or modify // it under the terms of the GNU Library General Public License as published by // the Free Software Foundation; either version 2, or (at your option) // any later version. // // The SC Toolkit 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 Library General Public License for more details. // // You should have received a copy of the GNU Library General Public License // along with the SC Toolkit; see the file COPYING.LIB. If not, write to // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. // // The U.S. Government is granted a limited license as per AL 91-7. // #include #include #include #include using namespace std; using namespace sc; // Force linkages: //#ifndef __PIC__ #ifdef PUMAGON # include static ForceLink fl0; #endif # ifdef HAVE_PTHREAD # include static ForceLink fl2; # endif //#endif class TestThread: public Thread { private: Ref lock; public: static int count; TestThread(const Ref &l): lock(l) {} void run(); int n() const { return 1000000; } }; int TestThread::count = 0; void TestThread::run() { for (int i=0; ilock(); count++; lock->unlock(); } } int main(int argc, char**argv) { int i; Ref grp = ThreadGrp::initial_threadgrp(argc, argv); Ref debugger; if (grp.null()) { const char* input = SRCDIR "/thrtest.in"; const char* keyword = "thread"; if (argc >= 2) input = argv[1]; if (argc >= 3) keyword = argv[2]; Ref keyval = new ParsedKeyVal(input); grp << keyval->describedclassvalue(keyword); debugger << keyval->describedclassvalue(":debug"); if (grp.null()) { cerr << scprintf("Couldn't initialize ThreadGrp\n"); abort(); } } if (debugger.nonnull()) { debugger->set_exec(argv[0]); debugger->set_prefix(0); } Debugger::set_default_debugger(debugger); TestThread **thr = new TestThread*[grp->nthread()]; Ref lock = grp->new_lock(); for (i=0; inthread(); i++) { thr[i] = new TestThread(lock); grp->add_thread(i,thr[i]); } grp->start_threads(); grp->wait_threads(); int right_count = 0; for (i=0; inthread(); i++) { right_count += thr[i]->n(); delete thr[i]; } delete[] thr; cout << "count = " << TestThread::count << " (expected " << right_count << ")" << endl; return 0; } ///////////////////////////////////////////////////////////////////////////// // Local Variables: // mode: c++ // c-file-style: "CLJ" // End: