namespace

Hi all,
I have the following code:
there is a comment that indicates changes to be made depending on the version of GCC. What should I bring as a change.

Thank you.

#ifndef INDRI_ATOMIC_HPP
#define INDRI_ATOMIC_HPP

#ifndef WIN32
#if HAVE_BITS_ATOMICITY_H
#include <bits/atomicity.h>
#endif
#if HAVE_EXT_ATOMICITY_H
#include <ext/atomicity.h>
#endif
#endif

namespace indri {
/*! \brief Atomic actions for thread support */
namespace atomic {
#ifdef WIN32
typedef volatile LONG value_type;

inline void increment( value_type& variable ) {
::InterlockedIncrement( &variable );
}

inline void decrement( value_type& variable ) {
::InterlockedDecrement( &variable );
}
#else
// GCC 3.4+ declares these in the __gnu_cxx namespace, 3.3- does not.
#if P_NEEDS_GNU_CXX_NAMESPACE
#define __atomic_add __gnu_cxx::__atomic_add
#endif
typedef _Atomic_word value_type;

inline void increment( value_type& variable ) {
__atomic_add( &variable, 1 );
}

inline void decrement( value_type& variable ) {
__atomic_add( &variable, -1 );
}
#endif
}
}

#endif // INDRI_ATOMIC_HPP


> What should I bring as a change.

1. Use a reasonably current version of the GNU compiler.

2. For minimal change to the rest of the program, rewrite the header as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef INDRI_ATOMIC_HPP
#define INDRI_ATOMIC_HPP

#include <atomic>

namespace indri {

    namespace atomic {

        typedef std::atomic<long> value_type;

        inline value_type increment( value_type& variable ) {

            return ++variable ;
        }

        inline value_type decrement( value_type& variable ) {

            return --variable ;
        }

    } // namespace atomic

} // namespace indri

#endif // INDRI_ATOMIC_HPP 


3. Compile with g++ -std=c++11 -Wall -Wextra -pedantic-errors
Last edited on
Thank you for your respense,
I had the following error when i executed my previous program with GCC V 4.4.4.3


from demo.cpp:185:
/usr/local/include/indri/atomic.hpp:
In function ‘void indri::atomic::increment(indri::atomic::value_type&)’:

/usr/local/include/indri/atomic.hpp:51: error: ‘__atomic_add’ was not declared in this scope

/usr/local/include/indri/atomic.hpp: In function ‘void indri::atomic::decrement(indri::atomic::value_type&)’:

/usr/local/include/indri/atomic.hpp:55: error: ‘__atomic_add’ was not declared in this scope

Thanks
Registered users can post here. Sign in or register to post.