1
我以GLib開始,並希望使用其GObject引用計數功能來跟蹤何時可以釋放線程之間共享的一段內存。我用例是如下:引用GLib計數內存塊
void sending_function() {
char *msg = create_message(); // Allocates some memory at the heap.
GObject *container = g_holds_my_pointer(msg, free);
for (int i = 0; i < num_threads; i++) {
g_object_ref(container);
sends_to_other_thread(other_thread, container);
}
}
void *other_thread(void *data) {
GObject *container = data;
char *msg = container->data;
// Do something with msg...
g_object_unref(container); // When the reference count reaches zero, frees msg.
}
是否有保存單個指針和引用計數達到0後通話免費上有一個簡單的容器對象?我試過用單個元素來使用GPtrArray,但容器不是被引用計數的GObject。另外,我不想爲這個用例聲明一個完整的GObject樣板文件。我認識到這是一個簡單的事情來實現自己 - 創建一個持有指針和原子計數器的結構 - 但我更喜歡已經測試過的實現,如果可能的話。
謝謝!它正是我所需要的,一開始就逃脫了我。 – 2014-09-19 18:57:21