473,748 Members | 2,523 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

initializeVaria bles();
try
{
Timer.start(int n);
performAlgorith m_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTi me();
}
catch (OutOfTimeExcep tion 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 2811
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:

initializeVaria bles();
try
{
Timer.start(int n);
performAlgorith m_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTi me();
}
catch (OutOfTimeExcep tion 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.programmi ng.threads'.

V
Jan 29 '06 #2

"Jakub Moskal" <ja**********@g mail.com> wrote in message
news:11******** ************@o1 3g2000cwo.googl egroups.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:

initializeVaria bles();
try
{
Timer.start(int n);
performAlgorith m_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTi me();
}
catch (OutOfTimeExcep tion 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.progr ammer, but SIGALRM should do the trick.
Jan 30 '06 #5
Neo
"Jakub Moskal" <ja**********@g mail.com> wrote in
news:11******** ************@o1 3g2000cwo.googl egroups.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:

initializeVaria bles();
try
{
Timer.start(int n);
performAlgorith m_1();
Timer.stop();
cout << "Running time for algorithm_1 was " << Timer.runningTi me();
}
catch (OutOfTimeExcep tion 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=endti me=0;}
int start(){ starttime = tickssincemidni ght();} //almost all
compilers have a variation of the above function int stop(){ stoptime
= tickssincemidni ght();} 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
2183
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 in the threadpool are blocked (deadlocked maybe)? As a followup qn to this.. (please excuse my ignorance here) Is there one threadpool per process or one threadpool for the *virtual machine* (ie runtime environment, sorry haven't got a diploma...
3
5970
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 ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads, after a while another 2 additional threads. With the 3 threads at start time we have totally 7 threads.
9
7876
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 might need to carry out some back-end processing without pausing the individual users' Session while that process runs. E.g. I might provide the ability for a user to upload a text file of job
20
2414
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 line 1 and Thread B handles call activity on telephone line 2. They use a common SQL datasource, but all DataSets are unique to each thread. Is there a way for thread A to occasionally communication to thread B that something has happened? ...
5
12247
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 event handlers may take longer than the interval for either of the timers, so it's possible for multiple events to fire "simultaneously" and for events to queue up. I'm attempting to get the timers to sync on some reference type object, or use...
10
1843
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 have been trying to make the program (wich has no UI) to keep running and let it execute everything once every few minutes (depending on how the timer is set). Can anyone tell me how I can get a timer in the program so the program keeps running?...
2
3560
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 is running, a timer function keeps executing every 250ms. The timer function does some real time data processing and generates a bitmap which needs to be displayed on a picture control. This is done using this->Invoke(...) in the form.
5
2128
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 active even when the service reaches it Service Timer Stop?
8
3370
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 on a network for files and then copies these files to another location (on the network) AND then updates a record in the database. The file copying is performed before the database update because the file system is not transactional. The code...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9555
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9329
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9250
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.