meganz / mingw-std-threads

Standard threads implementation currently still missing on MinGW GCC on Windows
BSD 2-Clause "Simplified" License
437 stars 136 forks source link

Trouble compiling with gcc 11.1 #79

Open MNRK01 opened 3 years ago

MNRK01 commented 3 years ago

Hi,

I have been trying to compile this code from boost 1.76.0 with mingw-w64 gcc 11:

/*  Copyright 2020 Rene Rivera
 *  Distributed under the Boost Software License, Version 1.0.
 *  (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 */

/*
This program is a compile test for support of C++11. If it compiles
successfully some key parts of C++11 the B2 engine requires are
available. This is used by the build script to guess and check the
compiler to build the engine with.
*/

// Some headers we depend on..
#include <thread>

int main()
{
    // Check for basic thread calls.
    { auto _ = std::thread::hardware_concurrency(); }
}

I get the following error with gcc 11.1, but not with gcc 10.3 or other earlier gcc 10:

In file included from C:/mingw-gcc-11.1.0/i686-1110-win32-sjlj-rt_v9-rev0/mingw32/opt/include/mingw-std-threads/thread:3,
                 from check_cxx11.cpp:14:
C:/mingw-gcc-11.1.0/i686-1110-win32-sjlj-rt_v9-rev0/mingw32/opt/include/mingw-std-threads/mingw.thread.h:330:24: error: 'class mingw_stdthread::thread' conflicts with a previous declaration
  330 | using mingw_stdthread::thread;
      |                        ^~~~~~
In file included from C:/mingw-gcc-11.1.0/i686-1110-win32-sjlj-rt_v9-rev0/mingw32/lib/gcc/i686-w64-mingw32/11.1.0/include/c++/thread:43,
                 from C:/mingw-gcc-11.1.0/i686-1110-win32-sjlj-rt_v9-rev0/mingw32/opt/include/mingw-std-threads/thread:2,
                 from check_cxx11.cpp:14:
C:/mingw-gcc-11.1.0/i686-1110-win32-sjlj-rt_v9-rev0/mingw32/lib/gcc/i686-w64-mingw32/11.1.0/include/c++/bits/std_thread.h:62:9: note: previous declaration 'class std::thread'
   62 |   class thread
      |         ^~~~~~

It seems this error is new to gcc 11. Commenting out using mingw_stdthread::thread on line 330 of mingw.thread.h allows me to get past this error, but causes downstream errors when building boost as one can expect.

I'll say that I m surprised that it appears no one else has seen this issue with mingw-w64 gcc 11. I'm wondering if this is on my end. I build my own gcc with scripts from mingw-builds.

Thank you for making mingw-std-threads available and also for any help that you can provide on this issue.

Jamaika1 commented 3 years ago

It looks like this to me. This is not a bug, but a deliberate action by the gcc developers not to use mingwstd under the pretext of using newer languages than C++11/14. Nobody's interested in improving mingwstd because there are no funds for it. In newer languages there are new functions that are not compatible with linux with windows. Did I manage to compile something with boost? Yes I compiled boost 1.77.0 beta with libwebp2. Is it working properly? Who knows that. https://github.com/Jamaika1/mingw_std_threads/ I do not take any responsibility for the given link.

MNRK01 commented 3 years ago

Hi,

Thanks for making https://github.com/Jamaika1/mingw_std_threads/ available. The thread part now works well with gcc 11.1 based on limited testing of the code in my original report.

However, I quickly ran into an issue trying to compile libicu which boost/regex needs optionally. The error is distilled down to the definition of class mutex being protected by #ifdef _GLIBCXX_HAS_THREADS in std_mutex.h. Since I am compiling gcc with win32 thread model, gthreads is not available to me and running:

printf '#include <mutex>\nstd::mutex test_mutex;int main(){return 0;}' | g++ -x c++ -E -dM - | grep -i THREAD

confirms that I don't have _GLIBCXX_HAS_THREADS defined.

I think that @Jamaika1's approach to the headers makes sense to me to avoid the namespace clash issues referenced above. It does appear that there is work needed to improve the code so that all the mingw-std-threads headers work well.

I should have pointed out in my initial report that I run the Powershell script Generate-StdLikeHeaders.ps1 under utility_scripts to use mingw-std-threads, i.e. I don't use CMake. Not sure if that is relevant to my error report.

I appreciate your work very much.

Jamaika1 commented 3 years ago

My mistake is that I defined it together. When we use MINGWSTD we don't use _GLIBCXX_HAS_GTHREADS. I split definitions. The most modified in GCC is definition . The modifications I have posted may be difficult to apply after a month. https://github.com/gcc-mirror/gcc/commits/master/libstdc%2B%2B-v3/include/std/type_traits It seems that the function mingw invoke doesn't apply when compiling C++11 in cpp if files don't contain thread. Interestingly this is just an internal function to mingw thread or it's a bug.

MNRK01 commented 3 years ago

It works much better now. I can build libicu and boost. I'm continuing to test other C++11 code. It looks like you no longer need to modify type_traits which is great news.

Is there any hope of backporting your code structure into this mingw-std-threads repo? Also, your code is setup for upstreaming into mingw-w64. Is that a possibility?

Thanks for working on it!