Thread: Telling a Thread to Stop (Can it be done better ?)

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Telling a Thread to Stop (Can it be done better ?)

    I'm basically using std::future and std:: promise in a way just opposite to how it was intended to be used(i.e what it seems to me).
    It works, but there should be a less kludgy way to do the same thing.

    Before yelling "Premature Optimization" note that I'd like to use a similar construct to play around with sorting algorithms.
    So, adding the extra check at every iteration may not be good....though the asymptotic complexity won't change (afaik).
    (After that feel free to say so )

    Code:
    #include<iostream>
    #include<future>
    #include<thread>
    #include<chrono>
    void foo(std::promise<bool>& p)
    {
        std::future<bool> f= p.get_future();
        long i=1;
        while(i++) //This is the loop I want to kill ...
            if(f.wait_for(std::chrono::seconds(0))==std::future_status::ready)
                break;
        std::cout<<"Counted to :"<<i<<std::endl;
        return ;
    }
    int main()
    {
        std::promise<bool> p;
        std::thread t(foo,std::ref(p));
        while(true)
        {
            char c;
            std::cout<<"$:";
            std::cin>>c;
            if(c=='q')
            {
                p.set_value(true); //...by sending a message or setting a flag from elsewhere/
                break;
            }
        }
        t.join();
        return 0;
    }
    Last edited by manasij7479; 08-20-2012 at 04:22 AM.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    You aren't really using `std:romise' or `std::future' in an "opposite" way. You don't really seem to be using them in any way I'd call semantically correct (normal, opposite, backwards, or forwards). It isn't the "ordering" that makes me think you've done this poorly. It is that you aren't actually "returning" anything to a "calling" function which is what these functions imply.

    I'd advise you to use a `std::conditional_variable' directly if you are only needing to "sequence" a particular kind of operation. Yes. That is all you are doing. If you don't see it, consider that you are only examining or setting a particular `bool' value. The variable referencing that `bool' value is owned by the shared `std:romise' object which just happens to be serializing (ordering) control to its member variables.

    You can wrap that mechanism if you don't want to do it manually or you feel you may need such a mechanism often, but then I'd probably tell you that if you need this often you are probably doing it wrong.

    If you have a "return a value" type situation you want `std:romise' and `std::future', but in general these situations will be packaged in such a particular fashion that the creation and management of these primitives are partially self-contained (which is why the standard provides such a facility).

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stop a thread
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-22-2008, 03:47 AM
  2. Stop button doesnt stop a function (using PostMessage)
    By scwizzo in forum Windows Programming
    Replies: 7
    Last Post: 03-07-2008, 07:54 PM
  3. Message box as thread to stop main thread
    By Yasir_Malik in forum Windows Programming
    Replies: 8
    Last Post: 04-11-2006, 11:07 AM
  4. Telling other applications to stop
    By nickname_changed in forum Windows Programming
    Replies: 11
    Last Post: 09-25-2003, 12:47 AM
  5. After how many posts do you stop following a thread?
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 03-03-2002, 11:55 PM

Tags for this Thread