/*In C++, a thread is a separate execution flow that can run concurrently with other
threads. Threads are useful for parallelizing work, such as performing multiple tasks
simultaneously or running a time-consuming task in the background while the main
thread continues to execute.*/
#include <iostream>
#include <thread>
void print_hello() {
for (int i = 0; i < 5; i++) {
std::cout << "Hello from thread 1!" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void print_world() {
for (int i = 0; i < 5; i++) {
std::cout << "Hello from thread 2!" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
}
}
int main() {//std::thread constructor is used to create the threads a1 and a2.
//These threads are launched simultaneously from the main thread
std::thread t1(print_hello);//there are 3 threads in this one is main and others are sub
std::thread t2(print_world);//each thread run same time at once
t1.join();//must we have to end sub threads before exiting main thread
t2.join();
return 0;
}
/*
The join function makes the main thread wait for a1 and a2 to finish their execution. Alternatively,
you can use detach if you want the threads to run independently of the main thread,
but this is not recommended in most cases as it can lead to unexpected behavior.*/
/*In this example, we create two threads t1 and t2 that run the print_hello and
print_world functions respectively. Both threads start executing immediately and
run in parallel. The sleep_for function is used to pause the execution of the
thread for a specified duration, in this case, 1 second.
It's important to note that the order of the output of the threads will be
different every time you run the program, as the scheduling of the threads is done
by the operating system and can vary.*/
/*In the context of C++ programming, when you create a new thread using std::thread, it represents
a new execution thread that runs independently of the main thread. If you do not join or detach the
created threads before the main thread exits, it can lead to several issues:
Termination of the Program: If the main thread finishes execution before the newly created threads,
the program might terminate prematurely, potentially causing the other threads to be abruptly terminated.
Resource Leakage: Threads might not be cleaned up properly, leading to resource leakage. This can result
in unjoined threads continuing to run even after the main thread has terminated, leading to memory leaks
or other resource-related issues.
The join() function is used to wait for a thread to finish its execution before the main thread continues.
This way, the main thread waits until the specified thread finishes its task. If you omit the join() call
and the main thread exits, the program might not wait for the child threads to complete, resulting in the
issues mentioned above.
In your example, calling join() ensures that the main thread waits for a1 and a2 to finish their
execution before it terminates. This helps to avoid premature termination and ensures the proper cleanup
of resources associated with the child threads.*/
To embed this project on your website, copy the following code and paste it into your website's HTML: