Comments (7)
Emm, I try your code under two OSes, Windows 10 and Ubuntu 16.04(Docker image)
My CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz (8 CPUs)
Windows: Microsoft Visual Studio 2017 v15.5.2
-
Should add macro
_ENABLE_ATOMIC_ALIGNMENT_FIX
in Preprcoessor Definitions. -
After compiling, running well under release mode.
Ubuntu : g++ (Ubuntu 5.4.0-6ubuntu1~16.04.5) 5.4.0 20160609
- I found you use
-march=native
option, while comipling and running well ... sometime .
root@fca31ff7994c:~# g++ -pthread -march=native -std=c++11 -o queue_mpmc queue_mpmc.cc
root@fca31ff7994c:~# ls
cmake queue_mpmc queue_mpmc.cc
root@fca31ff7994c:~# ./queue_mpmc
root@fca31ff7994c:~# ./queue_mpmc
Segmentation fault
root@fca31ff7994c:~# ./queue_mpmc
root@fca31ff7994c:~# ./queue_mpmc
When I add -g
want to debug, the Segmentation fault
not appeare.
root@fca31ff7994c:~# g++ -pthread -march=native -std=c++11 -g -o queue_mpmc queue_mpmc.cc
root@fca31ff7994c:~# ./queue_mpmc
root@fca31ff7994c:~# ./queue_mpmc
root@fca31ff7994c:~# ./queue_mpmc
root@fca31ff7994c:~# ./queue_mpmc
root@fca31ff7994c:~# ./queue_mpmc
root@fca31ff7994c:~# ./queue_mpmc
root@fca31ff7994c:~# ./queue_mpmc
While, this is a bug, may not find it quickly.
You would not care about this part. just some note.
Interesting, when I remove marchoption, like g++ -pthread -std=c++11 -o queue_mpmc queue_mpmc.cc
. I get compile-time error -- some link error
march
option information:
GCC 4.2 introduces a new -march option, -march=native, which automatically detects the features
your CPU supports and sets the options appropriately. If you have an Intel or AMD CPU and are using > >=sys-devel/gcc-4.2.3, using -march=native is recommended. Do not use -march=native if you use
distcc on nodes with different architectures as this may produce unusable code.
root@fca31ff7994c:~# g++ -pthread -std=c++11 -o queue_mpmc queue_mpmc.cc
/tmp/ccPyXNXz.o: In function `std::atomic<lock_free_queue_mpmc<int>::counted_node_ptr>::load(std::memory_order) const':
queue_mpmc.cc:(.text._ZNKSt6atomicIN20lock_free_queue_mpmcIiE16counted_node_ptrEE4loadESt12memory_order[_ZNKSt6atomicIN20lock_free_queue_mpmcIiE16counted_node_ptrEE4loadESt12memory_order]+0x2c): undefined reference to `__atomic_load_16'
/tmp/ccPyXNXz.o: In function `std::atomic<lock_free_queue_mpmc<int>::counted_node_ptr>::exchange(lock_free_queue_mpmc<int>::counted_node_ptr, std::memory_order)':
queue_mpmc.cc:(.text._ZNSt6atomicIN20lock_free_queue_mpmcIiE16counted_node_ptrEE8exchangeES2_St12memory_order[_ZNSt6atomicIN20lock_free_queue_mpmcIiE16counted_node_ptrEE8exchangeES2_St12memory_order]+0x4e): undefined reference to `__atomic_exchange_16'
/tmp/ccPyXNXz.o: In function `std::atomic<lock_free_queue_mpmc<int>::counted_node_ptr>::store(lock_free_queue_mpmc<int>::counted_node_ptr, std::memory_order)':
queue_mpmc.cc:(.text._ZNSt6atomicIN20lock_free_queue_mpmcIiE16counted_node_ptrEE5storeES2_St12memory_order[_ZNSt6atomicIN20lock_free_queue_mpmcIiE16counted_node_ptrEE5storeES2_St12memory_order]+0x3e): undefined reference to `__atomic_store_16'
/tmp/ccPyXNXz.o: In function `std::atomic<lock_free_queue_mpmc<int>::counted_node_ptr>::compare_exchange_strong(lock_free_queue_mpmc<int>::counted_node_ptr&, lock_free_queue_mpmc<int>::counted_node_ptr, std::memory_order, std::memory_order)':
queue_mpmc.cc:(.text._ZNSt6atomicIN20lock_free_queue_mpmcIiE16counted_node_ptrEE23compare_exchange_strongERS2_S2_St12memory_orderS5_[_ZNSt6atomicIN20lock_free_queue_mpmcIiE16counted_node_ptrEE23compare_exchange_strongERS2_S2_St12memory_orderS5_]+0x49): undefined reference to `__atomic_compare_exchange_16'
from cpp_concurrency_in_action.
I use ulimit -c unlimited
make core file dumped.
root@fca31ff7994c:~# ./queue_mpmc
Segmentation fault (core dumped)
use commend gdb -c core queue_mpmc
gdb show me, the error happen in pop function
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from queue_mpmc...(no debugging symbols found)...done.
[New LWP 19735]
[New LWP 19731]
[New LWP 19736]
[New LWP 19733]
[New LWP 19737]
[New LWP 19732]
[New LWP 19734]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./queue_mpmc'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000000000401163 in pop(lock_free_queue_mpmc<int>*) ()
[Current thread is 1 (Thread 0x7fa0d046b700 (LWP 19735))]
The time for bed, I will update this tomorrow. And you can use your core file to locate the bug.
0223 update:
Use debug also can get the error:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Core was generated by `./queue_mpmc'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000000000401742 in lock_free_queue_mpmc<int>::pop (this=0x7ffef540f3c0) at queue_mpmc.cc:167
167 if ( head.compare_exchange_strong( old_head,
[Current thread is 1 (Thread 0x7f44139c4700 (LWP 863))]
(gdb) l
162 {
163 ptr->release_ref();
164 return std::unique_ptr<T>();
165 }
166
167 if ( head.compare_exchange_strong( old_head,
168 ptr->next ) )
169 {
170 T* const res = ptr->data.exchange( nullptr );
171
(gdb) p ptr
$1 = (lock_free_queue_mpmc<int>::node * const) 0x0
The 154 line counted_node_ptr old_head = head.load( std::memory_order_relaxed );
old_head.ptr is nullptr.
This function would change the head.ptr address, make head.ptr to 0x0.
static void increase_external_count( std::atomic<counted_node_ptr>& counter, counted_node_ptr& old_counter)
{
counted_node_ptr new_counter;
do
{
new_counter=old_counter;
++new_counter.external_count;
} while( !counter.compare_exchange_strong(old_counter, new_counter, std::memory_order_acquire, std::memory_order_relaxed) );
old_counter.external_count = new_counter.external_count;
}
from cpp_concurrency_in_action.
Wow, thank you for attention.
Do you know, how to fix it? To be honest, i have no ideas :(
from cpp_concurrency_in_action.
Emm, multi-thread program is not easy to debug and fix relational bug. I will fix it, but need time. :)
from cpp_concurrency_in_action.
@Ilya21 sorry for late, focus on my job this days.
Today, I have time and see your question again.
When I run the program, the segmentation fault very quick today, unlike before.
I use gdb debug the program, found the relation pointer is nullptr in pop() function, like above show.
So I think the situation about pop behavior, pop is make the head node out of the queue.
Here, we use multi-thread, when the program want to pop, the queue have nothing.
May, this is the reason make pointer empty.
I change the pop code like below:
std::unique_ptr<T> pop()
{
counted_node_ptr old_head = head.load( std::memory_order_relaxed );
for(;;)
{
increase_external_count( head, old_head );
node* const ptr = old_head.ptr;
if (ptr == nullptr){
return std::unique_ptr<T>();
}
if ( ptr == tail.load().ptr)
{
ptr->release_ref();
return std::unique_ptr<T>();
}
if ( head.compare_exchange_strong( old_head,
ptr->next ) )
{
T* const res = ptr->data.exchange( nullptr );
free_external_counter( old_head );
return std::unique_ptr<T>( res );
}
ptr->release_ref();
}
}
Just add the judgment about head pointer.
The program running well now on my side.
You can try on your side, if have question you can feedback to me.
from cpp_concurrency_in_action.
typo: OS->OSes
from cpp_concurrency_in_action.
@FirstLoveLife ha, thanks your words. I fixed it.
from cpp_concurrency_in_action.
Related Issues (20)
- 第二章有句话感觉没有体现原文意思 HOT 2
- 清单3.4 线程安全的堆栈类定义错误 HOT 1
- 少了一个字符 HOT 1
- 3.2.3节问题 HOT 2
- 4.2.2 翻译的根本看不懂··· HOT 1
- 中文字体 HOT 1
- 第三章 3.2 有错字 HOT 1
- 请教一下有关atomic的执行顺序(5.3.3 原子操作的内存顺序 一节) HOT 4
- 3.2.3 节 选项2 HOT 1
- Pages can not be opened HOT 1
- 请教第五章里面的一个问题 HOT 8
- 原版 HOT 1
- 第二版出来了,还有兴趣翻译么? HOT 3
- 5.2.7 Free functions for atomic operations HOT 1
- 5.2.3 std::atomic<bool> 提供的exchange(), 不仅仅是一个“读-改-写”的操作 HOT 1
- 6.1 这被称为序列化(serialzation): 线程轮流访问被保护的数据 HOT 1
- 翻译的感觉还不如看英文呢老哥。。 HOT 1
- Now the font in the book becomes bigger, how to change it to be small ? HOT 1
- 第五章 <C++内存模型与原子操作> 有句话读不通 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cpp_concurrency_in_action.