繁体   English   中英

boehm-gc与C ++ 11的线程库

[英]boehm-gc with C++11's thread library

我们知道,使用贝姆-GC在多线程需要调用GC_register_my_thread从栈基GC_get_stack_base 但它似乎不能很好地与C ++ 11的线程库,如std::thread ...如何使用boehm-gc与C ++ 11的线程库?

(我使用VS2013)

编辑:这是经过测试的代码。 std::thread很好,但是std::future不起作用(停在_CrtIsValidHeapPointer

#include <iostream>
#include <thread>
#include <future>

#define GC_THREADS
#include <gc.h>
#include <gc_cpp.h>
#pragma comment(lib, "gcmt-lib")

void foo()
{
    GC_stack_base sb;
    GC_get_stack_base(&sb);
    GC_register_my_thread(&sb);

    int *ptr;
    for (int i = 0; i < 10; i++)
    {
        ptr = new (GC) int;
        *ptr = 1;
    }

    GC_unregister_my_thread();
}

int main()
{
    GC_INIT();
    GC_allow_register_threads();

    std::cout << "test for std::thread";
    std::thread thrd(foo);
    thrd.join();
    std::cout << " [sucs]\n";

    std::cout << "test for std::future";
    std::future<void> fu = std::async(std::launch::async, foo);
    fu.get();
    std::cout << " [sucs]\n";

    std::cin.get();
}

编辑:这里是一个堆栈跟踪的捕获(对不起,它不是英文,但我认为无所谓,无论如何) 在此输入图像描述

这是一个调试消息

HEAP[TestGC.exe]: Invalid address specified to RtlValidateHeap( 00E80000, 00C92F80 )

在调试时,我发现在fu.get()之后发生了错误。

编辑:/ MD(或/ MDd)不会发生错误...

(我认为GC可能会触及库的指针(namespcae Concurrency),但它只是猜测;;)

在开始使用收集器之前,在创建线程之前,请确保同时发出两者

  • GC_INIT ,和
  • GC_allow_register_threads

然后在每个帖子中跟进,

  • GC_get_stack_base/GC_register_my_thread ,最终
  • GC_unregister_my_thread

你没有说你正在编译什么,但它适用于gcc 4.8(使用-std = c ++ 11)。

编辑:OP能够通过解决上述指令并使用多线程动态MSVCR100运行时的/MD[d]标志编译代码来解决该问题。 多线程静态编译运行时的问题仍未解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM