473,324 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Timer, threads, exceptions

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

Jan 29 '06 #1
5 2786
Jakub Moskal wrote:
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
Jan 29 '06 #2

"Jakub Moskal" <ja**********@gmail.com> wrote in message
news:11********************@o13g2000cwo.googlegrou ps.com...
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


Jan 30 '06 #3
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

Jan 30 '06 #4
Jakub Moskal wrote:
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.
Jan 30 '06 #5
Neo
"Jakub Moskal" <ja**********@gmail.com> wrote in
news:11********************@o13g2000cwo.googlegrou ps.com:
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
Jan 31 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: steve | last post by:
Since System.Threading.Timer uses the threadpool to do its stuff what happens when (a) You try to create a timer and the thread pool is *exhausted* (b) The timer is due to fire AND all threads...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
9
by: Mark Rae | last post by:
Hi, I've seen several articles about using System Timers in ASP.NET solutions, specifically setting them up in Global.asax' Application_OnStart event. I'm thinking about the scenario where I...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
5
by: Tony Gravagno | last post by:
I have a class that instantiates two Timer objects that fire at different intervals. My class can be instantiated within a Windows Form or from a Windows Service. Actions performed by one of the...
10
by: Joris De Groote | last post by:
Hi, I have wrote a program that checks for files and moves these files to the correct folders when they come in. Everything works fine. However I always have to start that program manually. I...
2
by: Amit Dedhia | last post by:
Hi I am developing a scientific application which has moderate level image processing involved. In my application, there is a main application form which invokes another form. When this form...
5
by: John A. Bailo | last post by:
From a Windows service (NET 2.0) I want to launch serveral threads in a for loop that invokes a method using: new Thread(delegate() { myMethod(248);}).Start(); Will those threads stay...
8
by: Ollie Riches | last post by:
I'm looking into a production issue related to a windows service and System.Timers.Timer. The background is the windows service uses a System.Timers.Timer to periodically poll a directory location...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.