我们知道,在多线程中使用boehm-gc需要调用
(我使用VS2013)
编辑:这是经过测试的代码。
GC_register_my_thread
,并将栈基地址从GC_get_stack_base
传递过来。但是似乎不适用于C++11的线程库,例如std::thread
... 我该如何在C++11的线程库中使用boehm-gc?(我使用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)时不会出现错误...
(我认为垃圾回收可能会触及库的指针(命名空间并发),但这只是猜测;;)
std::async(std::launch::async, foo);
。 - mockinterfaceGC_win32_dll_threads
定义?gcmt-lib是如何构建的? 总体而言,看起来收集器正在拦截内部并发对象(MS c++11 lib实现细节)的创建,因此它被删除了两次。但我需要在装有msvc编译器的计算机上进行检查。 - mockinterfaceGC_
方法的返回值,例如,GC_register_my_thread()
是否返回了GC_SUCCESS
? - mockinterface