os/threads.cpp
Demonstrate the basic use of threads. See also os/ratethreadcpp
00001 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- 00002 00003 // Show thread basic functionalities, you may want to have a look at the 00004 // ratethread example. 00005 00006 // added initThread/releaseThread example -nat 00007 00008 #include <stdio.h> 00009 00010 #include <yarp/os/Thread.h> 00011 #include <yarp/os/Time.h> 00012 00013 using namespace yarp::os; 00014 00015 class Thread1 : public Thread { 00016 public: 00017 virtual bool threadInit() 00018 { 00019 printf("Starting thread1\n"); 00020 return true; 00021 } 00022 00023 virtual void run() { 00024 while (!isStopping()) { 00025 printf("Hello, from thread1\n"); 00026 Time::delay(1); 00027 } 00028 } 00029 00030 virtual void threadRelease() 00031 { 00032 printf("Goodbye from thread1\n"); 00033 } 00034 }; 00035 00036 class Thread2 : public Thread { 00037 public: 00038 virtual bool threadInit() 00039 { 00040 printf("Starting thread2\n"); 00041 return true; 00042 } 00043 00044 virtual void run() { 00045 Time::delay(0.5); 00046 while (!isStopping()) { 00047 printf("Hello from thread2\n"); 00048 Time::delay(1); 00049 } 00050 } 00051 00052 virtual void threadRelease() 00053 { 00054 printf("Goodbye from thread2\n"); 00055 } 00056 }; 00057 00058 int main() { 00059 Thread1 t1; 00060 Thread2 t2; 00061 printf("Starting threads...\n"); 00062 t1.start(); 00063 t2.start(); 00064 printf("started\n"); 00065 Time::delay(3); 00066 printf("stopping threads...\n"); 00067 t1.stop(); 00068 t2.stop(); 00069 printf("stopped\n"); 00070 return 0; 00071 } 00072
Generated on Thu Mar 11 11:01:42 2010 for YARP by
1.5.1