CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2019
    Posts
    28

    wait .. no blocking

    need examples of a proper way to wait for a flag change in another thread without blocking the events?. In C++/windows.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,109

    Re: wait .. no blocking

    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2019
    Posts
    28

    Re: wait .. no blocking

    how about condition variables versus events?. Both work at kernel level?. Which is more proper to use?.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,109

    Re: wait .. no blocking

    What " condition variables" do you mean?
    Victor Nijegorodov

  5. #5
    Join Date
    Aug 2019
    Posts
    28

    Re: wait .. no blocking

    This is what I mean?. Condition variables versus events.. ?
    https://en.cppreference.com/w/cpp/th...ition_variable

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,109

    Re: wait .. no blocking

    Well, both "condition variables" that you meant (std::mutex) and MS Event Objects (Synchronization) are synchronization objects. So I do not see any difference.
    However, I never used std::threads, so that is only my IMHO and I may be wrong.
    Victor Nijegorodov

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,564

    Re: wait .. no blocking

    Sorry, but I don't use std::threads either.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  8. #8
    Join Date
    Aug 2019
    Posts
    28

    Re: wait .. no blocking

    why you do not use std::threads?. What are other alternatives based on your experiences?. Specially for passing array of objects to it in C++. I read _beginthread(ex) are appropriate for C++.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,109

    Re: wait .. no blocking

    Because std::threads did NOT exist when we had begonnen using multithreading in MS Windows applications.
    We had _beginthread(ex)/_endthread(ex) to use in a plain c++/Win32 applications and a little more convenient AfxBeginThread for MFC.

    So why should we change/rewrite our good working since the decade code?
    Victor Nijegorodov

  10. #10
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,564

    Re: wait .. no blocking

    plus Windows events, mutexs, semaphores etc etc etc

    We only use Windows and our main threading code was written before C++11. We looked into moving to std::threads etc but we concluded that the cost of converting far outweighed any benefit to us.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++20 Compiler: Microsoft VS2022 (17.0.2)

  11. #11
    Join Date
    Feb 2017
    Posts
    634

    Re: wait .. no blocking

    Quote Originally Posted by @EE@ View Post
    need examples of a proper way to wait for a flag change in another thread without blocking the events?. In C++/windows.
    I often use a thread-safe producer-consumer queue for this kind of situation. The producer thread pushes information on the queue and the consumer thread pops it, either by active polling or waiting asleep.

    This is my code (inspired by the very good book "C++ Concurrency in Action" by Anthony Williams, 2 edition, page 179)

    Code:
    #ifndef LOCK_QUEUE_ONCE
    #define LOCK_QUEUE_ONCE
    
    //
    // Threadsafe queue
    //
    
    
    #include <queue>
    #include <memory>
    #include <condition_variable>
    #include <mutex>
    
    namespace maux {
    
    	template<typename T>
    	class Lockqueue  {
    		using Mutex = std::mutex;
    	public:
    		void push(T value) { // push
    			const std::lock_guard<Mutex> lock(mutex);
    			queue.push(std::move(value));
    			condition.notify_one();
    		}
    
    		bool try_pop(T& value) { // poll pop
    			std::lock_guard<Mutex> lock(mutex);
    			if (queue.empty()) return false;
    			value = std::move(queue.front());
    			queue.pop();
    			return true;
    		}
    
    		T wait_pop() { // wait pop
    			std::unique_lock<Mutex> lock(mutex);
    			condition.wait(lock, [this] () noexcept {return !queue.empty();});
    			T const value = std::move(queue.front());
    			queue.pop();
    			return value;
    		}
    
    		int size() const { // queue size
    			std::lock_guard<Mutex> lock(mutex);
    			return static_cast<int>(queue.size());
    		}
    
    	private:
    		mutable Mutex mutex;
    		std::queue<T> queue;
    		std::condition_variable condition;
    	};
    
    }
    
    #endif
    To use Lockqueue, the producer allocates a Lockqueue object on the heap using new. This pointer preferably (but not necessarily) is kept in a smart pointer called std::shared_ptr to ensure a safe automatic delete. It is thread-safe,

    https://docs.microsoft.com/en-us/cpp...?view=msvc-160

    Then the producer passes the (smart) pointer over to the consumer when it is started.

    Note that most algorithms of the C++ standard library can run in parallel today. It is no longer necessary to always manage own threads to achieve that.
    Last edited by wolle; October 26th, 2021 at 01:10 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured