Re: [Vala] volatile variable



On Mon, 2014-06-16 at 11:51 +0800, Nor Jaidi Tuah wrote:
[1] Note that because the libgee deals with pointers it needs to
implement a bit more. If you need a guide see
http://blog.piechotka.com.pl/2014/03/01/lock-free-collection-in-libgee-hazard-pointer/ (even more 
self-promotion)


Thanks for the link. That eventually leads me to
ConcurrentList. I can't use ConcurrentList directly
because I need these:

   void insert_after (G ref_item, G item)
   G get_after (G ref_item)

In non-threaded code, the above can be implemented using

   insert (index_of (ref_item) + 1, G);
   get (index_of (ref_item) + 1)

In multithreaded code, these won't work without locks.

Even though I cannot use ConcurrentList directly,
I hope I can learn something useful from it.


Nice day
Nor Jaidi Tuah


I don't want to worry you too much but what you need is a lock-free
list. While writing one is possible (ConcurrentList is a single-linked
lock free list) it's not a trivial exercise (I'd give it a few weeks at
least). 

A good news is that you could use a ConcurrentList - if you have an
iterator you can access the next element and insert the element after
the current one:

ListIterator<G>? find(List<G> g, EqualFunc<G> eq, G item) {
    var iter = g.list_iterator ();
    if (!iter.foreach ((i) => {return !eq (i, item);})) {
        return iter;
    } else {
        return null;
    }
}

bool insert_after(List<G> g, EqualFunc<G> eq, G item, G to_insert) {
    var iter = find (g, eq, item));
    if (iter != null) {
        iter.add (to_insert);
        return true;
    } else {
        return false;
    }
}

bool get_after(List<G> g, EqualFunc<G> eq, G item, out G? after) {
    var iter = find (g, eq, item));
    if (iter != null && iter.next ()) {
        after = iter.get ();
        return true;
    } else {
        after = null;
        return false;
    }
}

Both should be atomic. You might want to cache iterator as well (it
depends on your use-case). However locks might be faster as well
depending on your use-case.

Best regards

Attachment: signature.asc
Description: This is a digitally signed message part



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]