[英]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.