How can i run my program for a specific amount of time?
And what is threading?
I'm really new to C++ so i'd like n00b-friendly answers.
How can i run my program for a specific amount of time?
And what is threading?
I'm really new to C++ so i'd like n00b-friendly answers.
A few quick, practical points that build on what and started: threading means running multiple execution threads inside a single program (not multiple processes). Threads share the same memory, so you must design communication and stopping carefully. To run "for a specific amount of time" the safest approach is cooperative — let the worker detect a timeout and exit cleanly — rather than forcefully killing threads.
A common pattern in modern C++ (C++11+) is to spawn a worker thread and use an atomic flag that the main thread sets after the timeout. The worker checks the flag periodically and returns when set. This avoids undefined behavior from trying to terminate a thread abruptly.
#include <atomic>
#include <chrono>
#include <thread>
#include <iostream>
std::atomic<bool> stop{false};
void worker() {
while (!stop.load()) {
// do a small piece of work, then check stop again
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
std::cout << "worker stopped\n";
}
int main() {
std::thread t(worker);
std::this_thread::sleep_for(std::chrono::seconds(5)); // run 5 seconds
stop.store(true);
t.join();
} Notes and links: use std::chrono::steady_clock for measuring intervals (avoids system-clock jumps), prefer cooperative cancellation over forcing thread termination, and consider std::future with wait_for if you only need to detect a timeout rather than cancel work. See the standard threading docs for details: std::thread and synchronization and std::chrono::steady_clock.
Jump to Post— ff4930 24Threading is running multiple processes at once.
or translated, a program doing multiple tasks at the same time.
Because doing multiple things at the same time may affect certain things such as changing data.
If Thread A is changing data and Thread B comes across and also …
Threading is running multiple processes at once.
or translated, a program doing multiple tasks at the same time.
Because doing multiple things at the same time may affect certain things such as changing data.
If Thread A is changing data and Thread B comes across and also wants to change data, in order to avoid a collision, there is a method/a "lock", that all threads test to see if anyone is changing data, if there is, they wait.
You can google some Thread tutorials online.
You didn't answer my first question >.<
To run your program for a specific amout of time, you are correct you will need threading.
Once you understand threading, you may want to have a look at the ctime header
This will provide a method of checking how long the program has been running, you would set this check up in a different thread to everything else.
Chris
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.