t_thread.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_thread                                                       *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *  Purpose:                                                                   *
00007 *                                                                             *
00008 *    Checks that the ithread behaves properly.                                *
00009 *                                                                             *
00010 *******************************************************************************
00011 * Copyright (c) 1997-$now By Author.  This program is free software; you can  *
00012 * redistribute it and/or modify it under the terms of the GNU General Public  *
00013 * License as published by the Free Software Foundation; either version 2 of   *
00014 * the License or (at your option) any later version.  This is online at:      *
00015 *     http://www.fsf.org/copyleft/gpl.html                                    *
00016 * Please send any updates to: fred@gruntose.com                               *
00017 \*****************************************************************************/
00018 
00019 #include <basis/guards.h>
00020 #include <data_struct/amorph.cpp>
00021 #include <mechanisms/ithread.h>
00022 #include <mechanisms/time_stamp.h>
00023 #include <loggers/console_logger.h>
00024 #include <data_struct/static_memory_gremlin.h>
00025 
00026 #include <stdio.h>
00027 
00028 HOOPLE_STARTUP_CODE;
00029 
00030 const int MAX_SIMULTANEOUS_THREADS = 432;
00031   // we will create this many threads simultaneously.
00032 
00033 const int THREAD_INTERVAL = 84;  // how frequently the thread runs.
00034 
00035 #define PWL program_wide_logger()
00036 
00037 class one_shot : public ithread
00038 {
00039 public:
00040   one_shot() : ithread() {}
00041 
00042   virtual void perform_activity(void *) {
00043     PWL.log("one shot in.");
00044     portable::sleep_ms(20);
00045     PWL.log("one shot out.");
00046   }
00047 
00048 private:
00049 };
00050 
00051 class periodontic : public ithread
00052 {
00053 public:
00054   periodontic(int interval, timed_thread_types eval)
00055       : ithread(interval, eval) {}
00056 
00057   virtual void perform_activity(void *) {
00058     PWL.log(istring("bing at: ") + timestamp(false, true));
00059     portable::sleep_ms(200);
00060   }
00061 };
00062 
00063 class sporadicus : public ithread
00064 {
00065 public:
00066   sporadicus(timed_thread_types eval)
00067       : ithread(20, eval) {}
00068 
00069   virtual void perform_activity(void *) {
00070     PWL.log(".");
00071   }
00072 };
00073 
00074 class snoozer : public ithread
00075 {
00076 public:
00077   snoozer() : ithread(THREAD_INTERVAL, ithread::SLACK_INTERVAL) {}
00078 
00079   virtual void perform_activity(void *) {
00080     // do something.
00081     time_stamp quit;
00082   }
00083 };
00084 
00085 int main(int formal(argc), char *formal(argv)[])
00086 {
00087   SET_DEFAULT_CONSOLE_LOGGER;
00088 
00089   one_shot once_thread;
00090   once_thread.start(NIL);
00091   portable::sleep_ms(200);
00092   once_thread.stop();
00093   PWL.log("two messages should be above from one shot thread.\n");
00094 
00095   once_thread.start(NIL);
00096   portable::sleep_ms(200);
00097   once_thread.stop();
00098   PWL.log("two messages should be above from one shot thread.\n");
00099 
00100   periodontic tighty(1000, ithread::TIGHT_INTERVAL);
00101   tighty.start(NIL);
00102   portable::sleep_ms(10 * SECOND_ms);
00103   tighty.stop();
00104   PWL.log("messages above should tightly adhere to one second clicks.\n");
00105 
00106   periodontic slacky(1400, ithread::SLACK_INTERVAL);
00107   slacky.start(NIL);
00108   portable::sleep_ms(10 * SECOND_ms);
00109   slacky.stop();
00110   PWL.log("messages above should be about 1.4 seconds apart.\n");
00111 
00112   sporadicus spood(ithread::TIGHT_INTERVAL);
00113   PWL.eol(log_base::NO_ENDING);
00114   spood.start(NIL);
00115   portable::sleep_ms(14 * SECOND_ms);
00116   PWL.eol(log_base::CRLF_AT_END);
00117   PWL.log("");
00118   PWL.log("stopping spood");
00119   spood.stop();
00120   PWL.log("stopped spood");
00121 
00122   amorph<snoozer> snoozies;
00123   for (int i = 0; i < MAX_SIMULTANEOUS_THREADS; i++) {
00124     snoozer *newby = new snoozer;
00125     snoozies += newby;
00126   }
00127   PWL.log(isprintf("created %d threads.", snoozies.elements()));
00128   for (int i = 0; i < MAX_SIMULTANEOUS_THREADS; i++) {
00129     snoozies.borrow(i)->start(NIL);
00130   }
00131   PWL.log(isprintf("started %d threads.", snoozies.elements()));
00132 
00133   time_stamp when_to_leave(1 * MINUTE_ms);
00134   PWL.log("now running the threads...");
00135   while (time_stamp() < when_to_leave) {
00136     portable::sleep_ms(200);
00137   }
00138 
00139   guards::alert_message("ithread:: works for all functions tested (if messages seem appropriate).");
00140 
00141   return 0;
00142 }
00143 

Generated on Fri Nov 21 04:30:09 2008 for HOOPLE Libraries by  doxygen 1.5.1