15

マクロを介してアクセスされるすべてのスレッドに一意のポインターを格納する必要があります。シングルトンと静的な thread_local std::unique_ptr オブジェクトでこれを解決する必要があると思いました。コードの簡略版は次のとおりです。

main.cpp

#include <thread>
#include <vector>
#include <iostream>
#include <mutex>
using namespace std;

#include "yay.hpp"

mutex coutMutex;

void yay(int id)
{
    int* yayPtr = getYay();

    // I know this is bad
    coutMutex.lock();
    cout << "Yay nr. " << id << " address: " << yayPtr << endl;
    coutMutex.unlock();
}

int main()
{
    vector<thread> happy;
    for(int i = 0; i < thread::hardware_concurrency(); i++)
    {
        happy.push_back(thread(yay, i));
    }

    for(auto& smile : happy)
    {
        smile.join();
    }
    return 0;
}

yay.hpp

#ifndef BE_HAPPY
#define BE_HAPPY

#include <memory>
class Yay
{
    private:
        static thread_local std::unique_ptr<int> yay;
        Yay() = delete;
        Yay(const Yay&) = delete;
        ~Yay() {}
    public:
        static int* getYay()
        {
            if(!yay.get())
            {
                yay.reset(new int);
            }
            return yay.get();
        }
};

#define getYay() Yay::getYay()

#endif

yay.cpp

#include "yay.hpp"

thread_local std::unique_ptr<int>  Yay::yay = nullptr;

これを gcc 4.8.1 でコンパイルすると:

g++ -std=c++11 -pthread -o yay main.cpp yay.cpp

私は得る:

/tmp/cceSigGT.o: In function `_ZTWN3Yay3yayE':
main.cpp:(.text._ZTWN3Yay3yayE[_ZTWN3Yay3yayE]+0x5): undefined reference to `_ZTHN3Yay3yayE'
collect2: error: ld returned 1 exit status

clang からより多くの情報が得られることを期待していましたが、clang 3.4 では問題なく動作します。

clang++ -std=c++11 -pthread -o yay main.cpp yay.cpp

プログラムを実行すると、期待していた結果が得られます。

Yay nr. 2 address: 0x7fcd780008e0
Yay nr. 0 address: 0x7fcd880008e0
Yay nr. 1 address: 0x7fcd800008e0
Yay nr. 3 address: 0x7fcd700008e0
Yay nr. 4 address: 0x7fcd740008e0
Yay nr. 5 address: 0x7fcd680008e0
Yay nr. 6 address: 0x7fcd6c0008e0
Yay nr. 7 address: 0x7fcd600008e0

ここで何が間違っているのかわかりませんが、静的な thread_local unique_ptr オブジェクトを持つことはできませんか? int や「ネイキッド」ポインターなどの単純な型で機能します。

編集:

これは、 http: //gcc.gnu.org/bugzilla/show_bug.cgi?id=55800に関連するバグである可能性があります。

編集2:

回避策 1: 1 つのファイルを clang でコンパイルする (yay.cpp)

回避策 2 (恐ろしく、移植性がありません): 最初に yay.cpp をアセンブリにコンパイルし、追加します。

.globl _ZTWN3Yay3yayE
_ZTWN3Yay3yayE = __tls_init

アセンブリ ファイルへ、オブジェクト ファイルへのコンパイル、残りとのリンク

4

2 に答える 2