Pengenalan Struktur Data Bebas Kunci dengan Contoh Java

1. Pengenalan

Dalam tutorial ini, kita akan mengetahui apa struktur data yang tidak menyekat dan mengapa ia merupakan alternatif penting untuk struktur data serentak berasaskan kunci.

Pertama, kita akan membahas beberapa syarat seperti bebas halangan , bebas kunci , dan bebas menunggu .

Kedua, kita akan melihat asas blok algoritma bukan penyekat seperti CAS (bandingkan-dan-pertukaran).

Ketiga, kita akan melihat pelaksanaan antrian bebas kunci di Jawa, dan akhirnya, kita akan menguraikan pendekatan bagaimana mencapai kebebasan menunggu .

2. Lock Versus Kelaparan

Pertama, mari kita perhatikan perbezaan antara benang yang tersumbat dan kelaparan.

Dalam gambar di atas, Thread 2 memperoleh kunci pada struktur data. Apabila Thread 1 cuba mendapatkan kunci juga, ia perlu menunggu sehingga Thread 2 melepaskan kunci; ia tidak akan dilanjutkan sebelum mendapat kunci. Sekiranya kita menangguhkan Thread 2 semasa memegang kunci, Thread 1 harus menunggu selama-lamanya.

Gambar seterusnya menggambarkan kelaparan benang:

Di sini, Thread 2 mengakses struktur data tetapi tidak memperoleh kunci. Thread 1 cuba mengakses struktur data pada masa yang sama, mengesan akses serentak, dan kembali dengan segera, memberitahu utas bahawa ia tidak dapat menyelesaikan (merah) operasi. Thread 1 kemudian akan mencuba lagi sehingga berjaya menyelesaikan operasi (hijau).

Kelebihan pendekatan ini ialah kita tidak memerlukan kunci. Namun, apa yang boleh berlaku ialah jika Thread 2 (atau thread lain) mengakses struktur data dengan frekuensi tinggi, maka Thread 1 memerlukan sejumlah besar percubaan sehingga akhirnya berjaya. Kami memanggil kebuluran ini.

Nanti kita akan melihat bagaimana operasi membandingkan dan menukar mencapai akses tanpa sekatan.

3. Jenis Struktur Data Tidak Menyekat

Kita dapat membezakan antara tiga tahap struktur data yang tidak menyekat.

3.1. Bebas Halangan

Kebebasan halangan adalah bentuk struktur data yang tidak menyekat yang paling lemah. Di sini, kami hanya memerlukan benang dijamin untuk dilanjutkan jika semua utas lain digantung .

Lebih tepat lagi, utas tidak akan terus kelaparan jika semua utas lain digantung. Ini berbeza dengan menggunakan kunci dalam arti itu, bahawa jika benang menunggu kunci dan benang yang memegang kunci digantung, benang menunggu akan menunggu selamanya.

3.2. Bebas Kunci

Struktur data memberikan kebebasan kunci jika, pada bila-bila masa, sekurang-kurangnya satu utas dapat dilanjutkan . Semua benang lain mungkin kelaparan. Perbezaan kebebasan halangan adalah terdapat sekurang-kurangnya satu benang yang tidak kelaparan walaupun tidak ada benang yang digantung.

3.3. Tanpa Tunggu

Struktur data tidak menunggu jika bebas kunci dan setiap utas dijamin akan dilanjutkan setelah beberapa langkah, iaitu, utas tidak akan kelaparan untuk sejumlah langkah yang "tidak masuk akal besar".

3.4. Ringkasan

Mari kita ringkaskan definisi ini dalam perwakilan grafik:

Bahagian pertama gambar menunjukkan kebebasan halangan kerana Benang 1 (utas atas) dapat dilanjutkan (anak panah hijau) sebaik sahaja kami menangguhkan utas yang lain (di bahagian bawah berwarna kuning).

Bahagian tengah menunjukkan kebebasan mengunci. Sekurang-kurangnya Thread 1 dapat maju sementara yang lain mungkin kelaparan (panah merah).

Bahagian terakhir menunjukkan kebebasan menunggu. Di sini, kami menjamin bahawa Benang 1 dapat diteruskan (anak panah hijau) setelah tempoh kelaparan tertentu (anak panah merah).

4. Primitif Tidak Menyekat

Di bahagian ini, kita akan melihat tiga operasi asas yang membantu kita membina operasi bebas kunci pada struktur data.

4.1. Bandingkan dan Tukar

Salah satu operasi asas yang digunakan untuk mengelakkan penguncian adalah operasi membandingkan dan menukar (CAS) .

Idea perbandingan-dan-pertukaran adalah, bahawa pemboleh ubah hanya dikemas kini jika masih mempunyai nilai yang sama seperti pada waktu kita mengambil nilai pemboleh ubah dari memori utama. CAS adalah operasi atom, yang bermaksud bahawa pengambilan dan kemas kini bersama adalah satu operasi tunggal :

Di sini, kedua-dua utas memperoleh nilai 3 dari memori utama. Thread 2 berjaya (hijau) dan mengemas kini pemboleh ubah menjadi 8. Oleh kerana CAS pertama oleh thread 1 menjangkakan nilainya masih 3, CAS gagal (merah). Oleh itu, Thread 1 memperoleh nilai semula, dan CAS kedua berjaya.

Yang penting di sini adalah bahawa CAS tidak memperoleh kunci struktur data tetapi kembali benar jika kemas kini berjaya, jika tidak, ia kembali palsu .

Coretan kod berikut menggariskan bagaimana CAS berfungsi:

volatile int value; boolean cas(int expectedValue, int newValue) { if(value == expectedValue) { value = newValue; return true; } return false; }

We only update the value with the new value if it still has the expected value, otherwise, it returns false. The following code snippet shows how CAS can be called:

void testCas() { int v = value; int x = v + 1; while(!cas(v, x)) { v = value; x = v + 1; } }

We attempt to update our value until the CAS operation succeeds, that is, returns true.

However, it's possible that a thread gets stuck in starvation. That can happen if other threads perform a CAS on the same variable at the same time, so the operation will never succeed for a particular thread (or will take an unreasonable amount of time to succeed). Still, if the compare-and-swap fails, we know that another thread has succeeded, thus we also ensure global progress, as required for lock-freedom.

It's important to note that the hardware should support compare-and-swap, to make it a truly atomic operation without the use of locking.

Java provides an implementation of compare-and-swap in the class sun.misc.Unsafe. However, in most cases, we should not use this class directly, but Atomic variables instead.

Furthermore, compare-and-swap does not prevent the A-B-A problem. We'll look at that in the following section.

4.2. Load-Link/Store-Conditional

An alternative to compare-and-swap is load-link/store-conditional. Let's first revisit compare-and-swap. As we've seen before, CAS only updates the value if the value in the main memory is still the value we expect it to be.

However, CAS also succeeds if the value had changed, and, in the meantime, has changed back to its previous value.

The below image illustrates this situation:

Both, thread 1 and Thread 2 read the value of the variable, which is 3. Then Thread 2 performs a CAS, which succeeds in setting the variable to 8. Then again, Thread 2 performs a CAS to set the variable back to 3, which succeeds as well. Finally, Thread 1 performs a CAS, expecting the value 3, and succeeds as well, even though the value of our variable was modified twice in between.

This is called the A-B-A problem. This behavior might not be a problem depending on the use-case, of course. However, it might not be desired for others. Java provides an implementation of load-link/store-conditional with the AtomicStampedReference class.

4.3. Fetch and Add

Another alternative is fetch-and-add. This operation increments the variable in the main memory by a given value. Again, the important point is that the operation happens atomically, which means no other thread can interfere.

Java provides an implementation of fetch-and-add in its atomic classes. Examples are AtomicInteger.incrementAndGet(), which increments the value and returns the new value; and AtomicInteger.getAndIncrement(), which returns the old value and then increments the value.

5. Accessing a Linked Queue from Multiple Threads

To better understand the problem of two (or more) threads accessing a queue simultaneously, let's look at a linked queue and two threads trying to add an element concurrently.

The queue we'll look at is a doubly-linked FIFO queue where we add new elements after the last element (L) and the variable tail points to that last element:

To add a new element, the threads need to perform three steps: 1) create the new elements (N and M), with the pointer to the next element set to null; 2) have the reference to the previous element point to L and the reference to the next element of L point to N (M, respectively). 3) Have tail point to N (M, respectively):

What can go wrong if the two threads perform these steps simultaneously? If the steps in the above picture execute in the order ABCD or ACBD, L, as well as tail, will point to M. N will remain disconnected from the queue.

If the steps execute in the order ACDB, tail will point to N, while L will point to M, which will cause an inconsistency in the queue:

Of course, one way to solve this problem is to have one thread acquire a lock on the queue. The solution we'll look at in the following chapter will solve the problem with the help of a lock-free operation by using the CAS operation we've seen earlier.

6. A Non-Blocking Queue in Java

Let's look at a basic lock-free queue in Java. First, let's look at the class members and the constructor:

public class NonBlockingQueue { private final AtomicReference
    
      head, tail; private final AtomicInteger size; public NonBlockingQueue() { head = new AtomicReference(null); tail = new AtomicReference(null); size = new AtomicInteger(); size.set(0); } }
    

The important part is the declaration of the head and tail references as AtomicReferences, which ensures that any update on these references is an atomic operation. This data type in Java implements the necessary compare-and-swap operation.

Next, let's look at the implementation of the Node class:

private class Node { private volatile T value; private volatile Node next; private volatile Node previous; public Node(T value) { this.value = value; this.next = null; } // getters and setters }

Here, the important part is to declare the references to the previous and next node as volatile. This ensures that we update these references always in the main memory (thus are directly visible to all threads). The same for the actual node value.

6.1. Lock-Free add

Our lock-free add operation will make sure that we add the new element at the tail and won't be disconnected from the queue, even if multiple threads want to add a new element concurrently:

public void add(T element) { if (element == null) { throw new NullPointerException(); } Node node = new Node(element); Node currentTail; do { currentTail = tail.get(); node.setPrevious(currentTail); } while(!tail.compareAndSet(currentTail, node)); if(node.previous != null) { node.previous.next = node; } head.compareAndSet(null, node); // for inserting the first element size.incrementAndGet(); }

The essential part to pay attention to is the highlighted line. We attempt to add the new node to the queue until the CAS operation succeeds to update the tail, which must still be the same tail to which we appended the new node.

6.2. Lock-Free get

Similar to the add-operation, the lock-free get-operation will make sure that we return the last element and move the tail to the current position:

public T get() { if(head.get() == null) { throw new NoSuchElementException(); } Node currentHead; Node nextNode; do { currentHead = head.get(); nextNode = currentHead.getNext(); } while(!head.compareAndSet(currentHead, nextNode)); size.decrementAndGet(); return currentHead.getValue(); }

Again, the essential part to pay attention to is the highlighted line. The CAS operation ensures that we move the current head only if no other node has been removed in the meantime.

Java already provides an implementation of a non-blocking queue, the ConcurrentLinkedQueue. It's an implementation of the lock-free queue from M. Michael and L. Scott described in this paper. An interesting side-note here is that the Java documentation states that it's a wait-free queue, where it's actually lock-free. The Java 8 documentation correctly calls the implementation lock-free.

7. Wait-Free Queues

As we've seen, the above implementation is lock-free, however, not wait-free. The while loops in both the add and get method can potentially loop for a long time (or, though unlikely, forever) if there are many threads accessing our queue.

How can we achieve wait-freedom? The implementation of wait-free algorithms, in general, is quite tricky. We refer the interested reader to this paper, which describes a wait-free queue in detail. In this article, let's look at the basic idea of how we can approach a wait-free implementation of a queue.

A wait-free queue requires that every thread makes guaranteed progress (after a finite number of steps). In other words, the while loops in our add and get methods must succeed after a certain number of steps.

In order to achieve that, we assign a helper thread to every thread. If that helper thread succeeds to add an element to the queue, it will help the other thread to insert its element before inserting another element.

As the helper thread has a helper itself, and, down the whole list of threads, every thread has a helper, we can guarantee that a thread succeeds the insertion latest after every thread has done one insertion. The following figure illustrates the idea:

Of course, things become more complicated when we can add or remove threads dynamically.

8. Conclusion

Dalam artikel ini, kami melihat asas struktur data yang tidak menyekat. Kami menerangkan tahap dan operasi asas yang berbeza seperti membandingkan dan menukar .

Kemudian, kami melihat pelaksanaan dasar antrian bebas kunci di Jawa. Akhirnya, kami menggariskan idea bagaimana mencapai kebebasan menunggu .

Kod sumber lengkap untuk semua contoh dalam artikel ini terdapat di GitHub.