cameron314 / concurrentqueue

A fast multi-producer, multi-consumer lock-free concurrent queue for C++11
Other
9.53k stars 1.66k forks source link

When I use this queue, I find that the queue fails when I do cross file usage,thank you for your help! #304

Closed djhdj1 closed 2 years ago

djhdj1 commented 2 years ago

When I use this queue, I find that the queue fails when I do cross file usage. As an example: I created the queue under the a.h file: static moodycamel::ConcurrentQueue<int> inta(10);, and then in b.cppinta.try_enqueue(741852); int b = 0; inta.try_dequeue(b);cout << "\nb: " << b; the console will output 741852. But when I do only inta.try_enqueue(741852); in b.cpp and in c.cpp int b = 0;inta.try_dequeue(b);cout << "\nb: " << b ;The console will only output 0. I hope you can tell me which of my operations is wrong and how to use it if I want to use it across files, thanks!

cameron314 commented 2 years ago

static at file scope means the variable is defined only in the current translation unit. Every .cpp including that header is getting its own private queue.

If you have access to C++17, you can declare the variable inline instead of static. Otherwise, you'll have to declare the variable in the header and define it explicitly in a single .cpp.

djhdj1 commented 2 years ago

staticat 文件范围表示变量仅在当前翻译单元中定义。每个.cpp包括该标头)都有自己的专用队列。

如果您有权访问 C++17,则可以声明变量而不是 。否则,您必须在标头中声明变量,并在单个.cpp中显式定义它。inline``static

Thank You Very much!