What is the issue with std::async?

Near the beginning of this clip from C++ And Beyond, I heard something about problems with std::async . I have two questions: For a junior developer, is there a set of rules for what to do and what to avoid when using std::async ? What are the problems presented in this video? Are they related to this article? There are several issues: std::async without a launch policy lets the runtime

std :: async有什么问题?

从C ++ And Beyond开始,我听到了有关std::async问题。 我有两个问题: 对于初级开发人员,在使用std::async时,有什么规则可以避免? 此视频中出现了哪些问题? 他们是否与这篇文章有关? 有几个问题: 没有启动策略的std::async允许运行时库选择是启动一个新线程还是在将来调用get()或wait()的线程中运行任务。 正如Herb所说,这种情况很可能要使用。 问题在于,这会让它向运行时库的QoI开放,以便正确地获取线程

std::atomic bool or Normal global bool is good in single thread?

I have global vaiable in a below thread (bool allow) which restrict client to wait (by assign allow=false) till it is completed. After completed allow =true bool allow =true; static void * mythread(void *arg) { /* here is Lock mutex code */ allow =false;(**non atomic**) system(command); allow =true; /* here is Unlock Mutex code */ } if(allow) { // validate thread output } else

std ::原子布尔或普通全局布尔是好的单线程?

我有一个全局变量在下面的线程(布尔允许)限制客户端等待(通过分配允许= false),直到它完成。 完成后allow = true bool allow =true; static void * mythread(void *arg) { /* here is Lock mutex code */ allow =false;(**non atomic**) system(command); allow =true; /* here is Unlock Mutex code */ } if(allow) { // validate thread output } else { // wait } 我担心的是我应该使用std :: atomi

Calling SetEvent with an invalid handle

I have an application where a thread calls SetEvent and another thread waits for that event. However, CreateEvent was never called. The SetEven returns false (ERROR_INVALID_HANDLE), and the WaitForSingleObject returns WAIT_FAILED Yesterday, we commit something apparently not related : two lines of code in the .rc removing buttons from a toolbar. Suddently, on realease-XP-32 the WaitForSing

使用无效句柄调用SetEvent

我有一个应用程序,其中一个线程调用SetEvent,另一个线程等待该事件。 但是,CreateEvent从未被调用过。 SetEven返回false(ERROR_INVALID_HANDLE),WaitForSingleObject返回WAIT_FAILED 昨天,我们提交了一些显然不相关的东西:.rc从工具栏中删除按钮的两行代码。 突然,在realease-XP-32上, WaitForSingleObject返回WAIT_TIMEOUT 。 但我想知道: 1-为什么在资源中提交(显然不相关)确实改变了WaitForSingleObj

repeating WaitForSingleObject in messages between to applications

I created two programs in C (producer and consumer) which send messages the one to each other (using CreateEvent, SetEvent and WaitForSingleObject) I implement one thread for each program to manages this messages using WaitForSingleObject(myEvent, INFINITE) waiting for a message I put the WaitForSingleObject in looping to repeat and obtain many messages in time. This works fine in the first

在应用程序之间的消息中重复WaitForSingleObject

我在C(生产者和消费者)中创建了两个程序,它们互相发送消息(使用CreateEvent,SetEvent和WaitForSingleObject) 我为每个程序实现一个线程来使用WaitForSingleObject(myEvent,INFINITE)等待消息来管理这些消息 我把WaitForSingleObject放入循环中,以便及时重复并获取许多消息。 这在第一次工作正常,但其他时候WaitForSingleObject不等待消息,左代码运行在不定式循环中(包括WaitForSingleObject) 我测试删除两

Creating/Opening Events in C++ and checking if they are fired

I have two threads that use an event for synchronization. In each thread they use the same call: ::CreateEvent( NULL,TRUE,FALSE,tcEventName ) The producer thread is the one that makes the call first, while the consumer thread makes the call last, so it's technically opening, not creating the event... I assume. But, when SetEvent is called in the producer thread, the same event never gets

用C ++创建/打开事件并检查它们是否被触发

我有两个线程使用事件进行同步。 在每个线程中,他们使用相同的调用: ::CreateEvent( NULL,TRUE,FALSE,tcEventName ) 生产者线程是首先进行调用的线程,而消费者线程使得调用最后一次,所以它在技术上是开放的,而不是创建事件......我假设。 但是,当在生产者线程中调用SetEvent时,相同的事件永远不会在消费者线程中触发(我正在使用WaitForMultipleObjects()) 有没有一种工具可以告诉我事件是否真正被正确触发。

Error while decoding camera stream video frame using FFMpeg iOS

I have an app, In which I am decoding video frames coming from ip camera, but the method avcodec_decode_video2(m_pCodecCtx, m_pFrame, &consumed_bytes, &avpkt); present in avcodec.h generating bad_access, I can't figure out what is wrong. This app was working fine with old libraries FFMpeg, but by apple policy, in order to publish app it support arm64, so I updated my libraries to s

使用FFMpeg iOS解码相机流视频帧时出错

我有一个应用程序,其中我解码来自IP摄像头的视频帧,但方法avcodec_decode_video2(m_pCodecCtx, m_pFrame, &consumed_bytes, &avpkt); 目前在avcodec.h生成bad_access,我无法弄清楚什么是错的。 这个应用程序与旧库FFMpeg工作正常,但通过苹果政策,为了发布它支持arm64的应用程序,所以我更新了我的库以支持arm64,之后发生了此问题。 这是截图 虽然bad_access应用程序正在生成以下日志: [h264 @ 0x1071400]

C++11 how to observe memory order in atomic::store and atomic::load

Update 3 : After understanding what "memory order" is, I know the problem is totally not related to compiler. And yes, because my CPU architecture is Intel x86, no matter what code I write, the memory order effect will never happen . Update 2 : I check the disassembly code. However, I find no matter how I add code, the x.store always prior to y.store. The problem should come

C ++ 11如何观察atomic :: store和atomic :: load中的内存顺序

更新3 : 在了解了“内存顺序”之后,我知道问题与编译器完全无关。 是的,因为我的CPU架构是Intel x86, 无论我写什么代码,内存顺序效果都不会发生 。 更新2 : 我检查反汇编代码。 然而,我发现无论如何添加代码,x.store总是在y.store之前。 问题应该来自编译器(不重新排序这些代码)而不是CPU(据我所知)。 更新 : 看完评论后,似乎我不得不借用CPU为alpha,arm或ppc的机器。 有人知道我可以在哪里使用这

Waiting slots to be executed before quitting

I've a thread that read datas class MyThread: QThread { ... } void MyThread::run () { uint8_t* buffer; // in my real code, it's a ring, so there is not read during write // ... while (true) { if (isInterruptionRequested()) return; USB_READ(buffer); emit newData(buffer); } } In my UI Class I have: connect(this, &UIClass::newData, m_thread, &MyThread::

等待插槽在退出之前执行

我有一个读取数据的线程 class MyThread: QThread { ... } void MyThread::run () { uint8_t* buffer; // in my real code, it's a ring, so there is not read during write // ... while (true) { if (isInterruptionRequested()) return; USB_READ(buffer); emit newData(buffer); } } 在我的UI类中,我有: connect(this, &UIClass::newData, m_thread, &MyThread::newData); //

How to pass variadic args to a std::thread?

I would like to use my own Thread implementation by wrapping the std::thread class from C++11 so I will be able handle exceptions like I want. Here is my wrap class: #include <Types.hpp> #include <thread> #include <exception> #include <functional> class Thread { private: std::exception_ptr exceptionPtr; std::thread thread; public: usi

如何将可变参数传递给std ::线程?

我想通过从C ++ 11中包装std :: thread类来使用自己的Thread实现,这样我就可以处理像我想要的异常。 这是我的包装类: #include <Types.hpp> #include <thread> #include <exception> #include <functional> class Thread { private: std::exception_ptr exceptionPtr; std::thread thread; public: using Id = std::thread::id; using NativeHandleTy

Waking up thread from signal handler

I understand that about the only thing, a signal handler in ISO/C++11 is allowed to do is to read from or write to a lock free atomic variable or a volatile sig_atomic_t (I believe, POSIX is a little bit more permissive and allows to call a bunch of system functions). I was wondering, if there is any way, to wake up a thread that is waiting on a condition variable. Ie something like: #include

从信号处理程序唤醒线程

我明白关于唯一的事情,ISO / C ++ 11中的信号处理程序允许执行读取或写入锁定自由原子变量或volatile sig_atomic_t (我相信,POSIX是一个更宽容和允许调用一堆系统函数)。 我想知道,如果有什么办法,唤醒正在等待条件变量的线程。 即像是: #include <mutex> #include <atomic> #include <condition_variable> std::mutex mux; std::condition_variable cv; std::atomic_bool doWait{ true }; void