Skip to content

Commit

Permalink
*** empty log message ***
Browse files Browse the repository at this point in the history
svn path=/trunk/yarp2/; revision=6598
  • Loading branch information
lornat75 committed Mar 17, 2008
1 parent e201d08 commit c382b27
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 42 deletions.
4 changes: 2 additions & 2 deletions example/os/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PROJECT (Examples)

# replace "..." with the path to your YARP binaries if needed
SET(YARP_DIR "${CMAKE_SOURCE_DIR}/../.." CACHE LOCATION "where is yarp?")
#SET(YARP_DIR "${CMAKE_SOURCE_DIR}/../.." CACHE LOCATION "where is yarp?")

FIND_PACKAGE(YARP)

Expand All @@ -18,7 +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(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
55 changes: 22 additions & 33 deletions example/os/ratethread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,7 @@ class Thread1 : public RateThread {

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");
printf("Hello, from thread1\n");
}

virtual void threadRelease()
Expand Down Expand Up @@ -69,15 +62,6 @@ 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 @@ -95,44 +79,49 @@ 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");
return -1;
}
{
printf("One of the thread failed to initialize, returning\n");
return -1;
}

Time::delay(3);
printf("suspending threads...\n");
printf("Thread1 ran %d times, estimated rate: %.lf[ms]\n", t1.getIterations(), t1.getEstPeriod());
printf("Thread2 ran %d times, estimated rate: %.lf[ms]\n", t2.getIterations(), t2.getEstPeriod());

printf("suspending threads...\n");
t1.suspend();
// t2.suspend();
t2.suspend();

printf("Waiting some time");
for(int k=1;k<20;k++)
{
printf(".");
fflush(stdout);
Time::delay(0.2); //200[ms]
}
{
printf(".");
fflush(stdout);
Time::delay(0.2); //200[ms]
}
printf("\n");


printf("Changing thread1 rate to %d[ms]\n", 250);
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();
t2.resetStat();
t1.resume();
// t2.resume();
t2.resume();

Time::delay(3);

printf("Thread1 ran %d times, estimated rate: %.lf[ms]\n", t1.getIterations(), t1.getEstPeriod());
printf("Thread2 ran %d times, estimated rate: %.lf[ms]\n", t2.getIterations(), t2.getEstPeriod());
t1.stop();
// t2.stop();
t2.stop();
printf("stopped\n");

return 0;
Expand Down
39 changes: 32 additions & 7 deletions src/libYARP_OS/src/RateThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class RateThreadCallbackAdapter: public ThreadImpl
bool suspended;
double totalUsed; //total time taken iterations
unsigned int count; //number of iterations from last reset
unsigned int estPIt; //number of useful iterations for period estimation
double totalT; //time bw run, accumulated
double previousRun; //time when last iteration started
double currentRun; //time when this iteration started
Expand All @@ -48,6 +49,7 @@ class RateThreadCallbackAdapter: public ThreadImpl
{
totalUsed=0;
count=0;
estPIt=0;
totalT=0;
scheduleReset=false;
}
Expand All @@ -68,19 +70,34 @@ class RateThreadCallbackAdapter: public ThreadImpl

double getEstPeriod()
{
if (count<1)
return 0.0;
return totalT/(count);
double ret;
lock();
if (estPIt==0)
ret=0;
else
ret=totalT/estPIt;
unlock();
return ret;
}

unsigned int getIterations()
{ return count; }
{
lock();
unsigned int ret=count;
unlock();
return ret;
}

double getEstUsed()
{
double ret;
lock();
if (count<1)
return 0.0;
return totalUsed/count;
ret=0.0;
else
ret=totalUsed/count;
unlock();
return ret;
}

inline double getTime()
Expand All @@ -103,13 +120,18 @@ class RateThreadCallbackAdapter: public ThreadImpl

void singleStep()
{
lock();
currentRun=getTime();

if (scheduleReset)
_resetStat();

if (count>0)
totalT+=(currentRun-previousRun)*1000;
{
totalT+=(currentRun-previousRun)*1000;
estPIt++;
}

previousRun=currentRun;

if (!suspended)
Expand All @@ -128,6 +150,7 @@ class RateThreadCallbackAdapter: public ThreadImpl
else
sleep_period=0;

unlock();
Time::delay(sleep_period/1000.0);
#if 0
int us=sleep_period.usec()%1000;
Expand All @@ -145,7 +168,9 @@ class RateThreadCallbackAdapter: public ThreadImpl
{
while(!isClosing())
{
// lock();
singleStep();
// unlock();
}
}

Expand Down

0 comments on commit c382b27

Please sign in to comment.