1

I use a thread in order to provide a shell to user, in an OpenGL application.

My problem is that I can't cancel the thread at the end of my mainloop, because std::thread doesn't provide a cancel method, and my thread is blocked with a std::cin >> var call, so I can't use a boolean to store the fact that the thread should stop.

I would like to know if there is a good way of using std::cin in a thread (std::thread), or alternative solutions.

5
  • is your thread detached or joined Commented Nov 2, 2013 at 1:40
  • None. If I call thread.join() the application will wait the end of the thread, but the thread is blocked with std::cin call. I may not understand what is detaching a thread (detaching of the current process as fork() does ?) Commented Nov 2, 2013 at 1:43
  • Sounds like it's joined, after making a thread in c++11 you either call join or detach on it Commented Nov 2, 2013 at 1:44
  • do you have to read using >>? An asychronous io call might help (if not standardized). Commented Nov 2, 2013 at 3:19
  • Yes I use >> but I've found something: when I detach the thread in the constructor of the ThreadClass I wrote, i've problems; when I detach it in the mainloop, it works randomly: if the thread starts to be executed before beeing detached, it doesn't work, in the other case it works. Commented Nov 2, 2013 at 3:48

1 Answer 1

1

What you might want is an interrupting thread, c++ doesn't give you one but c++ concurrency in action has a simple implementation of one. That might be what you need. Wouldn't be surprised if boost also has one since the book is written by the maintainer of the thread library.

class interrupt_flag
    {
    public:
    void set();
        bool is_set() const;
    };
    thread_local interrupt_flag this_thread_interrupt_flag;
    class interruptible_thread
    {
        std::thread internal_thread;
        interrupt_flag* flag;
    public:
        template<typename FunctionType>
        interruptible_thread(FunctionType f)
        {
            std::promise<interrupt_flag*> p;
            internal_thread=std::thread([f,&p]{
                    p.set_value(&this_thread_interrupt_flag);
                    f(); 
                });
        flag=p.get_future().get();
    }
    void interrupt()
    {
        if(flag) {
            flag->set();
        }
    } 
};  

Now you can easily cancel the thread from your main. I put a link to the book but all the code from the book is also online for free. Here is a link to the source code in the book, though it may be hard to navigate without the book, which is a great read BTW.

Sign up to request clarification or add additional context in comments.

9 Comments

@KerrekSB: It shows me that I need an instance in my thread, which will send the exception.
internal_thread=std::thread([f,&p] ... -> I need to send a "pointer targeting a pointer targeting a flag" to my thread, and the thread initialize the adress.
The flag cannot be read uninitialized because of the check that it's initialized, as for how would it help I have to admit that I didn't read it too thoroughly but had remembered I saw it in the book, I though all he needed to be able to do is interrupt the thread from main
@KerrekSB you know the impl may not be 100% complete now that i look at it
@PierreEmmanuelLallemant btw it looks like boost thread library has an interruptible thread so you may want to just use that as it is likely more complete
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.