C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Timer, threads, exceptions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jakub Moskal
Guest





PostPosted: Sun Jan 29, 2006 8:00 pm    Post subject: Timer, threads, exceptions Reply with quote



Hi,

I want to write a benchmark that will measure performance of several
different algorithms for the same problem. There is a set time bound
though, the algorithm cannot run longer than n minutes.

Here is what the main program would do:

initializeVariables();
try
{
Timer.start(int n);
performAlgorithm_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTime();
}
catch (OutOfTimeException e)
{
cout << "Algorithm didin't finish in" << Timer.getN() << " minutes.";
}

The only way I see the timer to work and throw exception is to run it
as a thread in the background. If so, how do I stop the timer thread?
Any other ideas how to approach this problem?

Jakub
Back to top
Victor Bazarov
Guest





PostPosted: Sun Jan 29, 2006 9:00 pm    Post subject: Re: Timer, threads, exceptions Reply with quote



Jakub Moskal wrote:
Quote:
I want to write a benchmark that will measure performance of several
different algorithms for the same problem. There is a set time bound
though, the algorithm cannot run longer than n minutes.

Here is what the main program would do:

initializeVariables();
try
{
Timer.start(int n);
performAlgorithm_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTime();
}
catch (OutOfTimeException e)
{
cout << "Algorithm didin't finish in" << Timer.getN() << " minutes.";
}

The only way I see the timer to work and throw exception is to run it
as a thread in the background. If so, how do I stop the timer thread?
Any other ideas how to approach this problem?

None of that can be solved with C++. Threads are not part of the
language, they are platform-specific, and as such are OT here.

Try the newsgroup for your OS or 'comp.programming.threads'.

V
Back to top
Dave Townsend
Guest





PostPosted: Mon Jan 30, 2006 1:00 am    Post subject: Re: Timer, threads, exceptions Reply with quote



"Jakub Moskal" <jakub.moskal (AT) gmail (DOT) com> wrote in message
news:1138563676.276856.5680 (AT) o13g2000cwo (DOT) googlegroups.com...
Quote:
Hi,

I want to write a benchmark that will measure performance of several
different algorithms for the same problem. There is a set time bound
though, the algorithm cannot run longer than n minutes.

Here is what the main program would do:

initializeVariables();
try
{
Timer.start(int n);
performAlgorithm_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTime();
}
catch (OutOfTimeException e)
{
cout << "Algorithm didin't finish in" << Timer.getN() << " minutes.";
}

The only way I see the timer to work and throw exception is to run it
as a thread in the background. If so, how do I stop the timer thread?
Any other ideas how to approach this problem?

Jakub



I don' t think this will work the way you expect it to. I don't think the
exception can be caught by
your catch block since the exception would be thrown in a different thread.
Threads are not covered
by the C++ standard, so you might want to experiment to see if I'm right or
not.

There are a couple of ways to handle this problem:

1. Instrument the algorithm code to periodically check the time elapsed
since the start.
2. Instrument the algorithm code to check an "abort" flag which will be set
by a separate timer
thread.
3. Run the algorithm code in a separate thread and terminate the thread
after the max time has elapsed.
This might be platform specific, it would work for windows, but I'm not
familar with the threading
libraries on linux/unix.
4. Spawn off algorithms in their own processes, and monitor the process for
time. Kill the process when
the elapsed time is exceeded. This would be fairly easy to do on
Linux/Unix as a shell script, might
be a bit more involved with windows, either use Perl to monitor or
write a C++ program to do the
monitoring/killing.

dave
Back to top
Jakub Moskal
Guest





PostPosted: Mon Jan 30, 2006 1:00 am    Post subject: Re: Timer, threads, exceptions Reply with quote

Dave,

thank you for your reply. That's what I was afriad of, but I haven't
even got to that point to experiment with it. So here are my questions:

how can I spawn the algorithm and kill it after some time from c++
level in linux?

Do you think that using signals could solve it? fork the timer and send
a signal to the main program that also run the algorithm and handle the
signal?

Thank you for your help anyway!
Jakub
Back to top
red floyd
Guest





PostPosted: Mon Jan 30, 2006 5:00 am    Post subject: Re: Timer, threads, exceptions Reply with quote

Jakub Moskal wrote:
Quote:
Dave,

thank you for your reply. That's what I was afriad of, but I haven't
even got to that point to experiment with it. So here are my questions:

how can I spawn the algorithm and kill it after some time from c++
level in linux?

Do you think that using signals could solve it? fork the timer and send
a signal to the main program that also run the algorithm and handle the
signal?

Thank you for your help anyway!
Jakub


Your best bet is comp.unix.programmer, but SIGALRM should do the trick.
Back to top
Neo
Guest





PostPosted: Tue Jan 31, 2006 3:32 am    Post subject: Re: Timer, threads, exceptions Reply with quote

"Jakub Moskal" <jakub.moskal (AT) gmail (DOT) com> wrote in
news:1138563676.276856.5680 (AT) o13g2000cwo (DOT) googlegroups.com:

Quote:
Hi,

I want to write a benchmark that will measure performance of several
different algorithms for the same problem. There is a set time bound
though, the algorithm cannot run longer than n minutes.

Here is what the main program would do:

initializeVariables();
try
{
Timer.start(int n);
performAlgorithm_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTime();
}
catch (OutOfTimeException e)
{
cout << "Algorithm didin't finish in" << Timer.getN() << "
minutes.";
}

The only way I see the timer to work and throw exception is to run it
as a thread in the background. If so, how do I stop the timer thread?
Any other ideas how to approach this problem?

Jakub


Hmm.. heres a simple idea.. for a timer class.

class timer{
int starttime;
int endtime;

public:
timer() { starttime=endtime=0;}
int start(){ starttime = tickssincemidnight();} //almost all
compilers have a variation of the above function int stop(){ stoptime
= tickssincemidnight();} int timeelapsed() {return
(endtime-starttime);}
}

You could add exceptions accordingly.

Nice day
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.