This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Compiling for single-threaded use (implicit threading support difference in 4.9.1 vs. 4.8.1)


On 15 October 2014 10:14, Jonathan Wakely wrote:
> On 14 October 2014 18:29, Johan Alfredsson wrote:
>> Hi,
>>
>> I've noticed that g++ 4.9.1 behaves differently than 4.8.1 with
>> regards to (implicit) threading support. The 4.8.1 and 4.9.1 compilers
>> used were configured with identical options (*) to the configure
>> script (except --prefix) using --enable-threads=posix.
>>
>> For the following test-case
>>
>> #include <string>
>> #include <iostream>
>>
>> int main() {
>>     std::string test("test");
>>     std::cout << test << std::endl;
>> }
>>
>> invoking g++ -O3 test.cc -o test, the 'test' binary is compiled with
>> multi-threading support using 4.9.1 but not using 4.8.1, e.g. for the
>> libstdc++ pool allocator a mutex is locked when allocating memory for
>> the string in the test program above while no such locking is present
>> in the 'test' binary compiled with 4.8.1. (There is also a difference
>> in that there is a weak symbol __pthread_key_create in the binary
>> compiled with 4.9.1 but no such thing for the  4.8.1 case.)
>
> Using a mutex in a single-threaded program would be a bug.
>
>> As my application is single-threaded, I don't want to pay the
>> performance penalty of mutexes etc. Hence, my question is if it is
>> possible to explicitly request gcc to compile my application in
>> single-threaded mode.
>
> It should happen automatically, there's no way to request it because
> there should be no need.
>
> I'll try to reproduce what you're seeing.

I can't reproduce the problem with GCC 4.9.1 or trunk. I'm using a
Fedora 20 x86_64 system, so it's possible there's something different
on your distro.

The code below should be equivalent to what you're running, but
without depending on --enable-libstdcxx-allocator=
pool

#include <string>
#include <iostream>
#include <ext/pool_allocator.h>

int main() {
    std::basic_string<char ,std::char_traits<char>,
__gnu_cxx::__pool_alloc<char> > test("test");
    std::cout << test << std::endl;
}


I don't see any mutex locking or atomic operations because
__gthread_active_p() always returns false.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]