-1

1.数日前に質問( weak_ptrのスレッドセーフについて)を投稿しましたが、現在、他の関連する質問があります。このようなことをすると、上記の例で g_w として競合状態が発生しますか? (私のプラットフォームは ms vs2013 です)

std::weak_ptr<int> g_w;

void f3()
{
    std::shared_ptr<int>l_s3 = g_w.lock(); //2. here will read g_w
    if (l_s3)
    {
        ;/.....
    }
}

void f4() //f4 run in main thread
{
    std::shared_ptr<int> p_s = std::make_shared<int>(1);
    g_w = p_s;

    std::thread th(f3); // f3 run in the other thread
    th.detach();
    // 1. p_s destory will motify g_w (write g_w)
}

2.私が知っているように、std::tr1::shared_ptr/weak_ptrから派生したstd::shared_ptr/weak_ptr、およびboost::shared_ptr/weak_ptrから派生したstd::tr1::shared_ptr/weak_ptrは、実装に違いはありますか?特に、スレッドセーフの救済において。

4

2 に答える 2

0

@ケーシー

まず、コードを完成させます。

int main()
{
  f4();
  getchar();
  retrun 0;
}

Visual Studio 2013 でいくつかのコードを見つけました。 レースはありますか?

于 2013-12-25T04:40:47.547 に答える