c++ - g_hash_table 和 OpenMp

标签 c++ c openmp glib

g_hash_table_lookupg_hash_table_insert 是线程安全的吗?我可以使用这样的代码吗:

dict = g_hash_table_new();
for (i = 0; i < N; i++) {
    compute_A();
    find_hash_of_A();
    void *value = g_hash_table_lookup(dict, key);
    struct MyStruct *obj;
    if (!value) {
        obj = (struct MyStruct *)value;
    } else {
        compute_obj
        g_hash_table_insert(dict, key, obj);
    }
    do_something_with_obj
}

使用 #pragma omp parallel for,或者我需要使用其他一些 OpenMP 编译指示? 有时我在那个循环中遇到错误。一个线程版本工作正常。

最佳答案

没有。来自Threads section of the GLib Reference Manual :

GLib itself is internally completely thread-safe (all global data is automatically locked), but individual data structure instances are not automatically locked for performance reasons. For example, you must coordinate accesses to the same GHashTable from multiple threads. The two notable exceptions from this rule are GMainLoop and GAsyncQueue, which are thread-safe and need no further application-level locking to be accessed from multiple threads. Most refcounting functions such as g_object_ref() are also thread-safe.

关于c++ - g_hash_table 和 OpenMp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12771591/

相关文章:

c++ - 尽管更改了字符串,但我的字符串返回了某个数字

c++ - 将 Windows 商店中的私钥转换为 PEM(用于 OpenSSL)

python - 将 C 函数的指针参数与 python 脚本中的 ctype 参数链接起来

c++ - 使用 openmp 进行 LU 分解

c++ - openMP:从并行区域调用并行函数

C-OpenMP/尝试将递归代码与任务并行化,但速度较慢

c++ - 使用 Valgrind 的 std::fpclassify for long double 的错误结果

c++ - 根据一些枚举值在编译时添加额外的方法

c - 如何从C中的结构中打印指针变量?

c++ - C C++ 中的 SysRc 枚举值