-
October 16th, 2021, 10:15 PM
#1
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.
-
October 17th, 2021, 03:14 AM
#2
Re: wait .. no blocking
Victor Nijegorodov
-
October 17th, 2021, 08:41 PM
#3
Re: wait .. no blocking
how about condition variables versus events?. Both work at kernel level?. Which is more proper to use?.
-
October 18th, 2021, 02:08 AM
#4
Re: wait .. no blocking
What " condition variables" do you mean?
Victor Nijegorodov
-
October 20th, 2021, 01:53 AM
#5
Re: wait .. no blocking
This is what I mean?. Condition variables versus events.. ?
https://en.cppreference.com/w/cpp/th...ition_variable
-
October 20th, 2021, 02:47 AM
#6
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
-
October 20th, 2021, 03:13 AM
#7
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 21st, 2021, 11:29 PM
#8
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++.
-
October 22nd, 2021, 03:32 AM
#9
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
-
October 22nd, 2021, 03:40 AM
#10
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++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 23rd, 2021, 03:34 AM
#11
Re: wait .. no blocking
Originally Posted by @EE@
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|