Code Monkey home page Code Monkey logo

Comments (7)

xiaoweiChen avatar xiaoweiChen commented on May 2, 2024

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.

xiaoweiChen avatar xiaoweiChen commented on May 2, 2024

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.

Ilya21 avatar Ilya21 commented on May 2, 2024

Wow, thank you for attention.
Do you know, how to fix it? To be honest, i have no ideas :(

from cpp_concurrency_in_action.

xiaoweiChen avatar xiaoweiChen commented on May 2, 2024

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.

xiaoweiChen avatar xiaoweiChen commented on May 2, 2024

@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.

FirstLoveLife avatar FirstLoveLife commented on May 2, 2024

typo: OS->OSes

from cpp_concurrency_in_action.

xiaoweiChen avatar xiaoweiChen commented on May 2, 2024

@FirstLoveLife ha, thanks your words. I fixed it.

from cpp_concurrency_in_action.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.