Skip to content

missing raii_thread, detached_thread #471

Closed

Activity

  1. neilmacintosh commented on Mar 24, 2017

    @neilmacintosh
    Contributor

    Thanks for pointing that out Sergey Zubkov (@cubbimew). It's nice to have an issue to track getting the implementation done.

    They are somewhat down the implementation priority list at the moment, unless anyone wants to submit a PR.

  2. galik commented on Mar 24, 2017

    @galik
    Contributor

    note: edited to flesh out a few missing pieces.

    I happen to have these laying about that have not been tested to any real degree yet. I don't know if this is what they were intended to be. Real world template classes seem to be a whole lot more complicated looking than the ones I make...

    class detached_thread
    {
    public:
    	template<typename Callable, typename... Args>
    	explicit detached_thread(Callable&& f, Args&&... args)
    	: t(std::forward<Callable>(f), std::forward<Args>(args)...) {t.detach();}
    
            // other pass-through functions
    
    private:
    	std::thread t;
    };
    
    class raii_thread
    {
    	friend void swap(raii_thread& t1, raii_thread& t2) noexcept;
    	
    public:
    	raii_thread() noexcept = default;
    	
    	raii_thread(raii_thread const&) = delete;
    	raii_thread(raii_thread&& other): t(std::move(other.t)) {}
    
    	raii_thread(std::thread const&) = delete;
    	raii_thread(std::thread&& other) noexcept: t(std::move(other)) {}
    
    	raii_thread& operator=(raii_thread const&) = delete;
    	raii_thread& operator=(raii_thread&& other) noexcept { t = std::move(other.t); return *this; }
    	
    	raii_thread& operator=(std::thread const&) = delete;
    	raii_thread& operator=(std::thread&& other) noexcept { t = std::move(other); return *this; }
    
    	template<typename Callable, typename... Args>
    	explicit raii_thread(Callable&& f, Args&&... args)
    	: t(std::forward<Callable>(f), std::forward<Args>(args)...) {}
    
    	~raii_thread() { if(t.joinable()) t.join(); }
    	
    	bool joinable() const { return t.joinable(); }
    	
    	std::thread::id get_id() const noexcept { return t.get_id(); }
    	
    	std::thread::native_handle_type native_handle() { return t.native_handle(); }
    	
    	void join() { t.join(); }
    	
    	void swap(raii_thread& other) noexcept { using std::swap; swap(t, other.t); }
    
    private:
    	std::thread t;
    };
    
    void swap(raii_thread& t1, raii_thread& t2) noexcept
    {
    	using std::swap;
    	swap(t1.t, t2.t);
    }

    If there is more to it than this then you probably want to have real expert provide something more appropriate. 😄

  3. neilmacintosh commented on Apr 2, 2017

    @neilmacintosh
    Contributor

    Galik (@galik) I appreciate any starting point ;-)

    Would it be possible to throw together a PR containing these? That way they pass through the CLA process, so we get the legal clearance to use the code (not that I'm accusing you of trying to do anything shady, just trying to dot my i's and cross my t's). Having them as a PR would also make it easier for people to review\contribute to their evolution as necessary.

  4. galik commented on Apr 2, 2017

    @galik
    Contributor

    Neil MacIntosh (@neilmacintosh) Sure. I just posted what code I currently had here to make sure I had the functionality correct. Regarding gsl::detached_thread it doesn't seem to make much sense to implement moving, copying or joining. In fact the sooner the object dies the better as far as I can see. Would I delete the irrelevant functions or keep them to mimic equivalence with std::thread?

  5. added a commit that references this issue on Jan 15, 2019
  6. JordanMaples commented on Aug 26, 2020

    @JordanMaples
    Contributor

    Maintainers' call: We never got around to implementing gsl::joining_thread, now that C++20 has std::jthread, please use that instead. Sorry this took so long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions