Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 this thread should then be stopped only.
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 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

What I have tried:

C++
#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;
}
Posted
Updated 27-Feb-20 0:07am
v2
Comments
Richard MacCutchan 27-Feb-20 5:11am    
That thread will run recursively for ever; what exactly are you trying to achieve with this code?
Member 12899279 27-Feb-20 5:18am    
i know but there's something inside that function which i need to run after set interval of time lets say 5 minutes
i need this function to call itself again and again after 5minutes thats why i have dedicated a separate thread for it because i want it to run seprately from the other sequential code in my main()
i need this thread to stop and concurrently this function from executing too as soon as all the other sequential code is finished executing in the main and in main i am on return 0
Richard MacCutchan 27-Feb-20 5:55am    
You need to kill the thread from main, or use a flag that main sets to indicate that the thread should terminate.

it was very simple and easy i don't know why this thought didn't enter in my mind to simply use a bool flag i guess i was overthinking on a very simple task anyhow below is the code
using namespace std;
bool stop_thread_1 = false;

void do_something(/*int i*/)
{
	int sleep_time = 1000;

	/*int count = i*/;
	while (stop_thread_1==false)
	{
		cout << "\ni am running after 1 minute in thread" << endl;

		Sleep(sleep_time);
	
	}
}
void simpleFunc()
{
	cout << "\nsimple function to check";
}
int main()
{
	cout << "\nIn main";
	thread th1(do_something);
	simpleFunc();
	cout << "\nafter the thread in main";
	Sleep(6000);
	cout << "\nafter Sleep in main";

	stop_thread_1 = true;
	th1.join();
	
	system("pause");
	exit(0);
	
}
 
Share this answer
 
I suppose you can rearrange your approach, avoiding recursion. The following program
C++
#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
using namespace std;
using namespace std::chrono_literals;

void do_something()
{
  cout << "do something..." << endl;
}

void control_thread(std::atomic<bool> & stop)
{
  while (!stop)
  {
    this_thread::sleep_for(1s);
    do_something();
  }
}

int main()
{
  std::atomic<bool> stop{false};

  thread ct(control_thread, ref(stop));

  for (size_t n = 0; n<10; ++n)
  {
    cout << "main thread, iteration " << n << ", sleeping..." << endl;;
    this_thread::sleep_for(2.5s);
  }

  stop = true;
  cout << "wating for the thread to join..." << endl;
  ct.join();

  cout << "finished" << endl;
}

produces
main thread, iteration 0, sleeping...
do something...
do something...
main thread, iteration 1, sleeping...
do something...
do something...
main thread, iteration 2, sleeping...
do something...
do something...
do something...
main thread, iteration 3, sleeping...
do something...
do something...
main thread, iteration 4, sleeping...
do something...
do something...
do something...
main thread, iteration 5, sleeping...
do something...
do something...
main thread, iteration 6, sleeping...
do something...
do something...
do something...
main thread, iteration 7, sleeping...
do something...
do something...
main thread, iteration 8, sleeping...
do something...
do something...
do something...
main thread, iteration 9, sleeping...
do something...
do something...
wating for the thread to join...
do something...
finished
 
Share this answer
 
Comments
Member 12899279 27-Feb-20 6:14am    
thanks!can you please point out which approach is better your's or mine and why?
your code is quiet large and a little bit of difficult then mine but i am sure your's must be better as i am a beginner
CPallini 27-Feb-20 6:23am    
Mine of course, because it is working :-D
On the serious side, recursion is bad here.
Member 12899279 27-Feb-20 6:36am    
no you didn't get my point just before your solution i have posted mine above and in that i am no more using recursion
CPallini 28-Feb-20 6:05am    
You solution as fine as it correspond to your requirements (Possibly I had misenderstood suche requirements).
Member 12899279 28-Feb-20 6:01am    
i have got another question what happens if there was some error or exception in main()which caused not to reach till the end of main and exited with exception what happens to the thread then because thread stop is dependent on the bool value which is just above return 0.
what if never reaches that bool value?will it run forever until a pc restarts?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900