Skip to content

Commit

Permalink
*** empty log message ***
Browse files Browse the repository at this point in the history
svn path=/trunk/yarp2/; revision=6584
  • Loading branch information
lornat75 committed Mar 13, 2008
1 parent 1f2c5ca commit 62fa338
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 55 deletions.
1 change: 1 addition & 0 deletions example/os/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ ADD_EXECUTABLE(threads threads.cpp)
ADD_EXECUTABLE(image_process image_process.cpp)
ADD_EXECUTABLE(image_process_module image_process_module.cpp)
ADD_EXECUTABLE(ratethread ratethread.cpp)
ADD_EXECUTABLE(ratethreadtiming ratethreadTiming.cpp)
ADD_EXECUTABLE(make_count make_count.cpp)
ADD_EXECUTABLE(view_count view_count.cpp)
ADD_EXECUTABLE(browse_bottle browse_bottle.cpp)
Expand Down
79 changes: 79 additions & 0 deletions example/os/rateThreadTiming.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-


// rate thread example -nat

#include <stdio.h>

#include <yarp/os/RateThread.h>
#include <yarp/os/Time.h>
#include <yarp/os/Thread.h>

#include <yarp/sig/Matrix.h>
#include <yarp/math/Rand.h>

using namespace yarp::os;
using namespace yarp::math;
using namespace yarp::sig;

const int NROWS=100;
const int NCOLS=100;

const int THREAD_PERIOD=4;
const int MAIN_WAIT=3;

class Thread1 : public RateThread {
Matrix m;
public:
Thread1(int r):RateThread(r){}
virtual bool threadInit()
{
printf("Starting thread1\n");
return true;
}

//called by start after threadInit, s is true iff the thread started
//successfully
virtual void afterStart(bool s)
{
if (s)
printf("Thread1 started successfully\n");
else
printf("Thread1 did not start\n");


m.resize(NROWS,NCOLS);
}

virtual void run()
{
if (getIterations()==5)
{
double estP=getEstPeriod();
double estU=getEstUsed();
fprintf(stderr, "Thread1 est dT:%.3lf[ms]\n", estP);
fprintf(stderr, "Thread1 est used:%.3lf[ms]\n", estU);
resetStat();
}

for(int r=0;r<m.rows();r++)
for(int c=0;c<m.cols();c++)
m[r][c]=Rand::scalar(-1,1);
}

virtual void threadRelease()
{
printf("Goodbye from thread1\n");
}
};

int main() {
Thread1 t1(THREAD_PERIOD);

t1.start();

Time::delay(MAIN_WAIT);

t1.stop();
return 0;
}
31 changes: 25 additions & 6 deletions example/os/ratethread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ class Thread1 : public RateThread {
printf("Thread1 did not start\n");
}

virtual void run() {
virtual void run()
{
if (getIterations()==3)
{
double estP=getEstPeriod();
fprintf(stderr, "Thread1 est dT:%.3lf[ms]\n", estP*1000);
resetStat();
}

printf("Hello, from thread1\n");
}

Expand Down Expand Up @@ -61,6 +69,15 @@ class Thread2: public RateThread {

virtual void run()
{
int d=getIterations();
fprintf(stderr, "run:%d\n",d);
if (d==3)
{
double estP=getEstPeriod();
fprintf(stderr, "Thread2 est dT:%.3lf[ms]\n", estP*1000);
resetStat();
}

printf("Hello, from thread2\n");
}

Expand All @@ -78,7 +95,7 @@ int main() {

printf("Starting threads...\n");
bool ok=t1.start();
ok = ok&&t2.start();
// ok = ok&&t2.start();
if (!ok)
{
printf("One of the thread failed to initialize, returning\n");
Expand All @@ -89,7 +106,7 @@ int main() {
printf("suspending threads...\n");

t1.suspend();
t2.suspend();
// t2.suspend();

printf("Waiting some time");
for(int k=1;k<20;k++)
Expand All @@ -104,16 +121,18 @@ int main() {
printf("Changing thread2 rate to %d[ms]\n", 500);

t1.setRate(250);
t2.setRate(500);
// t2.setRate(500);

printf("Resuming threads...\n");
t1.resetStat();
// t2.resetStat();
t1.resume();
t2.resume();
// t2.resume();

Time::delay(3);

t1.stop();
t2.stop();
// t2.stop();
printf("stopped\n");

return 0;
Expand Down
22 changes: 21 additions & 1 deletion src/libYARP_OS/include/yarp/os/RateThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,27 @@ class yarp::os::RateThread {
*/
void resume();


/**
* Reset thread statistics.
*/
void resetStat();

/**
* Return estimated period since last reset.
*/
double getEstPeriod();

/**
* Return the number of iterations performed since last reset.
*/
unsigned int getIterations();

/**
* Return the estimated duration of the run() function since
* last reset.
*/
double getEstUsed();

/**
* Called just before a new thread starts.
*/
Expand Down
Loading

0 comments on commit 62fa338

Please sign in to comment.