Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

panda/src/express/test_threaddata.cxx

Go to the documentation of this file.
00001 // Filename: test_threaddata.cxx
00002 // Created by:  cary (16Sep98)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) 2001, Disney Enterprises, Inc.  All rights reserved
00008 //
00009 // All use of this software is subject to the terms of the Panda 3d
00010 // Software license.  You should have received a copy of this license
00011 // along with this source code; you will also find a current copy of
00012 // the license at http://www.panda3d.org/license.txt .
00013 //
00014 // To contact the maintainers of this program write to
00015 // panda3d@yahoogroups.com .
00016 //
00017 ////////////////////////////////////////////////////////////////////
00018 
00019 #include "pandabase.h"
00020 #include "thread.h"
00021 #include "pmutex.h"
00022 #include "mutexHolder.h"
00023 #include "pointerTo.h"
00024 
00025 Mutex *cout_mutex = (Mutex *)NULL;
00026 
00027 // Test forking a thread with some private data.
00028 class ThreadWithData : public Thread {
00029 public:
00030   ThreadWithData(const string &name, int parameter);
00031 
00032   virtual void thread_main();
00033 
00034 private:
00035   int _parameter;
00036 };
00037 
00038 
00039 ThreadWithData::
00040 ThreadWithData(const string &name, int parameter) : 
00041   Thread(name),
00042   _parameter(parameter)
00043 {
00044   MutexHolder holder(cout_mutex);
00045   cout << "Creating thread " << get_name() << " with parameter " << _parameter
00046        << "\n";
00047 }
00048 
00049 void ThreadWithData::
00050 thread_main() {
00051   for (int i = 0; i < _parameter; i++) {
00052     {
00053       MutexHolder holder(cout_mutex);
00054       cout << "Running thread " << get_name()
00055            << " with parameter " << _parameter
00056            << ", i = " << i << "\n" << flush;
00057       Thread *thread = get_current_thread();
00058       nassertv(thread == this);
00059     }
00060     Thread::sleep((double) i / 10.0);
00061   }
00062 }
00063 
00064 int
00065 main() {
00066   cout << "main beginning.\n";
00067   for (int i = 0; i < 10; i++) {
00068     string name = string("thread_") + (char)(i + 'a');
00069     PT(Thread) thread = new ThreadWithData(name, i);
00070     if (!thread->start(TP_low, false, false)) {
00071       MutexHolder holder(cout_mutex);
00072       cout << "Unable to start " << name << ".\n";
00073     } else {
00074       MutexHolder holder(cout_mutex);
00075       cout << "Started " << name << ", count = " 
00076            << thread->get_ref_count() << "\n";
00077     }
00078   }
00079 
00080   {
00081     MutexHolder holder(cout_mutex);
00082     cout << "main preparing to exit.\n";
00083   }
00084 
00085   Thread::prepare_for_exit();
00086 
00087   cout << "main exiting.\n";
00088 
00089   return 0;
00090 }

Generated on Fri May 2 00:38:36 2003 for Panda by doxygen1.3