Rasoul
Rasoul

Reputation: 51

Check if a thread is finished to send another param to it

I wanna to check if a thread job has been finished to call it again and send another parameter to that. The code is sth like this:

void SendMassage(double Speed)
{
    Sleep(200);
    cout << "Speed:" << Speed << endl;

}

int main() {

    int Speed_1 = 0;
    thread f(SendMassage, Speed_1);

    for (int i = 0; i < 50; i++)
    {
        Sleep(20);

        if (?)
        {
            another call of thread // If last thread done then call it again, otherwise not.
        }

        Speed_1++;
    }

}

How should I do it?

Upvotes: 0

Views: 86

Answers (3)

Daniel Langr
Daniel Langr

Reputation: 23527

Use, e.g., an atomic flag to indicate that the thread has finished:

std::atomic<bool> finished_flag{false};

void SendMassage(double Speed) {
   Sleep(200);
   cout << "Speed:" << Speed << endl;
   finished_flag = true;    
}

int main() {    
   int Speed_1 = 0;
   thread f(SendMassage, Speed_1);

   while (Speed_1 < 50) {
      Sleep(20);    
      if (finished_flag) {
         f.join();
         finished_flag = false;             
         f = std::thread(SendMassage, Speed_1);
      }          
      Speed_1++;    
   }
   f.join();
}

Working example: https://wandbox.org/permlink/BrEMHFvlInshBy5V


Note that I assumed that, according to your code, you don't want to block when checking whether the thread f has finished. Otherwise, simply call f.join().

Upvotes: 2

Aconcagua
Aconcagua

Reputation: 25536

The possibly most simple way of doing so is just joining the thread. Nothing clever, but...

OK, but why would you then want to have another thread at all if your main thread passes all its time sleeping anyway, so you quite sure are looking for something cleverer.

I personally like the principle of queues; you could use e. g. a std::deque for:

Your producer thread places in some values, your consumer thread just takes them out. Of course, you need to protect your queue via a std::mutex (or by other appropriate means) against race conditions...

The consumer would be running in an endless loop, processing the queue, if entries are available, or sleep if this is not the case. Have a look at this response for how to do the waiting...

There is the danger, though, that your queue runs full, so you might define some threshold when you stop or at least slow down producing new values, if you discover your producer being too fast. The queue has another advantage, though: If your producer is too fast, you might have more than one consumer, all serving the same queue (depending on your needs, putting together the results might need some extra efforts to keep ordering of correct).

Admitted, that's quite some work to do, it might be worth the effort, it might be overkill. If simpler approaches fit your needs already, Daniel's answer is fine, too...

Upvotes: 0

Kaldrr
Kaldrr

Reputation: 2850

If you want to wait untill a thread has finished it's job without using Sleep, you neeed to call it's join method, like so

thread t(SendMassage, Speed_1);
t.join();
//Code here will start executing after returning from join

You can read more about it here http://en.cppreference.com/w/cpp/thread/thread/join

About sending another parameter, I think the best way would be splitting it into another function that you would call after this thread has been joined, if you need some information about something that's known only inside the function, you could create a class that would store that information in it's fields, and use it in the function you're threading.

Upvotes: 0

Related Questions