cond_select.C

00001 #include <unistd.h>
00002 #include <string>
00003 #include <iostream>
00004 
00005 #include <archon/util/exception.H>
00006 #include <archon/util/thread.H>
00007 
00008 using namespace std;
00009 using namespace Archon::Utilities;
00010 
00011 struct T: Thread
00012 {
00013   Mutex m;
00014   Condition c;
00015 
00016   T(): c(m) {}
00017         
00018   void main()
00019   {
00020     fd_set r, w, x;
00021     FD_ZERO(&r);
00022     FD_ZERO(&w);
00023     FD_ZERO(&x);
00024     FD_SET(0, &r);
00025     Time t = Time::now();
00026     t += Time(8.0);
00027     char b[1024];
00028     for(;;)
00029     {
00030       int n;
00031       {
00032         Mutex::Lock l(m);
00033         n = c.select(1, &r, &w, &x, t);
00034       }
00035       if(n==-1)
00036       {
00037         cerr << "INTERRUPTED\n";
00038         //break;
00039       }
00040       else if(n==0)
00041       {
00042         cerr << "TIMEOUT\n";
00043         break;
00044       }
00045       else
00046       {
00047         n = read(0, b, 1024);
00048         if(!n) break;
00049         cerr << "> " << string(b, n);
00050       }
00051     }
00052     cerr << "QUIT\n";
00053   }
00054 
00055   ~T()
00056   {
00057     wait();
00058   }
00059 };
00060 
00061 int main()
00062 {
00063   set_unexpected(Exception::terminal<exceptionCatchInfo>);
00064   set_terminate (Exception::terminal<exceptionCatchInfo>);
00065 
00066   Ref<T> t = new T;
00067 
00068   Thread::start(t);
00069 
00070   for(unsigned i=0; i<3; ++i)
00071   {
00072     Thread::sleep(Time(1.0));
00073     cerr << "--" << i << "--\n";
00074   }
00075 
00076   t->terminate();
00077 
00078   /*
00079   t.c.notifyAll();
00080   Thread::sleep(Time(1.0));
00081   t.c.notifyAll();
00082   Thread::sleep(Time(1.0));
00083   t.c.notifyAll();
00084   Thread::sleep(Time(1.0));
00085   t.c.notifyAll();
00086   */
00087         
00088   t->wait();
00089 
00090   return 0;
00091 }

Generated on Sun Jul 30 22:55:43 2006 for Archon by  doxygen 1.4.4