Question:
c++ how to set a certain amount of time a process runs for?
spark
2010-10-21 02:54:11 UTC
Just wondering how i want to make my program run for 60 seconds then exit. can anyone supply me with some example code, thanks.
Three answers:
Cubbi
2010-10-21 06:03:47 UTC
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

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

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

#include

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

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

#include

#include

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.
?
2016-12-02 09:15:30 UTC
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.
oops
2010-10-21 03:26:56 UTC
#include



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

#include



#include



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

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...