S
Shahzaib17
Guest
in c++ main i want to send start a thread at the start of the program
this thread is supposed to call a function again and again until the end of the program s reached which means when every other thing is done in the main below this thread then this thread should be stopped only.(not before that)
i need this thread to call a function recursively because i need to do something inside this function after specific time lets say after every 2 minutes i want this function to execute itself mean while other functions in main are being completed sequentially.
After every function completes sequentially i want the thread to stop and i can't get any logic to do that below is my code i am unable to stop the thread with std::terminate()
i think think the problem is because of infinite loop in which function is calling itself recursively
i cannot think of any logic to do that
#include<iostream>
#include<chrono>
#include<thread>
#include<functional>
//#include<unistd.h>
#include<Windows.h>
using namespace std;
void do_something()
{
cout << "\ni am running after 1 minute in thread" << endl;
int sleep_time = 1000;
while (true)
{
Sleep(sleep_time);
do_something();
}
}
void simpleFunc()
{
cout << "\nsimple function to check";
}
int main()
{
cout << "\nIn main";
//thread t1(timer_start(do_something()), 1);
//while (true);
thread th1(do_something);
simpleFunc();
cout << "\nafter the thread in main";
Sleep(6000);
cout << "\nafter Sleep in main";
//th1.detach();
//th1.detach();
//th1.join();
std::terminate();
return 0;
//std::terminate();
//system("pause");
//return 0;
}
Continue reading...
this thread is supposed to call a function again and again until the end of the program s reached which means when every other thing is done in the main below this thread then this thread should be stopped only.(not before that)
i need this thread to call a function recursively because i need to do something inside this function after specific time lets say after every 2 minutes i want this function to execute itself mean while other functions in main are being completed sequentially.
After every function completes sequentially i want the thread to stop and i can't get any logic to do that below is my code i am unable to stop the thread with std::terminate()
i think think the problem is because of infinite loop in which function is calling itself recursively
i cannot think of any logic to do that
#include<iostream>
#include<chrono>
#include<thread>
#include<functional>
//#include<unistd.h>
#include<Windows.h>
using namespace std;
void do_something()
{
cout << "\ni am running after 1 minute in thread" << endl;
int sleep_time = 1000;
while (true)
{
Sleep(sleep_time);
do_something();
}
}
void simpleFunc()
{
cout << "\nsimple function to check";
}
int main()
{
cout << "\nIn main";
//thread t1(timer_start(do_something()), 1);
//while (true);
thread th1(do_something);
simpleFunc();
cout << "\nafter the thread in main";
Sleep(6000);
cout << "\nafter Sleep in main";
//th1.detach();
//th1.detach();
//th1.join();
std::terminate();
return 0;
//std::terminate();
//system("pause");
//return 0;
}
Continue reading...