Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
c++ how to set a certain amount of time a process runs for?
Just wondering how i want to make my program run for 60 seconds then exit. can anyone supply me with some example code, thanks.
3 Answers
- ?Lv 71 decade agoFavorite Answer
Don't smoke your CPU with a while(clock() < time) kinds of loops.
Depending on your compiler and OS, you can run your code on a separate thread while sleeping in main() or set up a timer:
thread solutions:
C++0x (the newest revision of the C++ language):
#include <thread>
void process()
{
// your code goes here
}
int main()
{
std::thread t(process);
std::this_thread::sleep_for( std::chrono::minutes(1) );
}
(tested with gcc 4.4.5)
C++98 (the oldest revision of the C++ language) with boost libraries (www.boost.org)
#include <boost/thread.hpp>
void process()
{
// your code goes here
}
int main()
{
boost::thread t(process);
boost::this_thread::sleep( boost::posix_time::minutes(1) );
}
C++98 on a POSIX OS (such as Linux)
#include <pthread.h>
#include <unistd.h>
void* process(void* arg)
{
// your code goes here
return arg;
}
int main()
{
pthread_t id;
pthread_create(&id, NULL, process, NULL);
sleep(60);
}
C++98 on Windows OS
#include <windows.h>
DWORD WINAPI process(void)
{
// your code here
return 0;
}
int main()
{
DWORD id;
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) process, NULL, 0, &id);
Sleep(60*1000);
}
alarm timer solutions:
C++98 on a POSIX OS
#include <unistd.h>
#include <signal.h>
#include <time.h>
int main()
{
timer_t timerid;
timer_create(CLOCK_REALTIME, NULL, &timerid);
itimerspec its = {{0, 0}, {60, 0}};
timer_settime(timerid, 0, &its, NULL);
// your code goes here.
}
I think Windows has its own timer event, WM_TIMER, too, but that's enough for one post.
- ?Lv 44 years ago
evidence is for arithmetic. technological expertise deals with evidence. i've got considered how creationists "interpret" the evidence to make it help creationism. that's humorous while it is not so stupid which you're banging your head on the table. no individual who makes use of the old argument approximately including counsel has ever been waiting to outline "counsel" in a fashion it is clever to a scientist. it particularly is merely hand-waving that impresses creationists who understand no longer something approximately technological expertise. Geneticists can element to various approaches wherein mutations upload genetic variety, that's probably what they think of "counsel" is yet won't particularly say so. given which you're a creationist i'm going to flow forward and assume that like various different creationist i've got ever run into, you haven't any longer have been given any activity in data, evidence or logical arguments. you merely desire to evangelise. We become uninterested in writing out long, informational solutions purely to have them skipped over.
- oopsLv 61 decade ago
#include <ctime>
int main()
{
clock_t end_time = clock() + 60 * CLOCKS_PER_SEC;
while(clock() < end_time)
{
// your code here
}
}
If your code is more linear in nature, and you don't want to constantly check the time, you just want to end your program abruptly, you can use threads. Here's an example using boost, which if you don't have, you should. http://www.boost.org/
#include <ctime>
#include <iostream>
#include <boost/thread.hpp>
void threadmain()
{
// Your main program here
}
int main()
{
boost::thread th(threadmain);
clock_t end_time = clock() + 5 * CLOCKS_PER_SEC;
while(clock() < end_time)
{}
// program ends abruptly, whether the other thread is done or not
}