ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Resource Sharing, Critical Sections, and Atomic operations
    Modeling/Architecture 2020. 2. 27. 00:02

    1. Overview

    Resource sharing is multi-thread architecture design.

    2. Resource Sharing

    2.1 What is a resource

    • Variables (Integers, Strings, and etc.)
    • Data Structures
    • File or Connection handles
    • Message or work queues
    • Any Objects

    2.2 Why share resources

    2.2.1 Case 1

    In this case, the benefit from sharing this data structure is that the document saver thread can save the user from losing his or her work if the application crashes or if we lose power.

    2.2.2 Case 2

    Having one thread called the dispatcher thread that takes work as input either directly from the user or through HTTP requests. If it's a web server for example and distributes the work among a small number of dedicated worker threads using a shared queue, the worker threads are waiting for the work to arrive through that queue and grab the task from it as soon as they finish their current task that queue here is obviously backed by a data structure stored on the heap and is a shared resource this leads to very efficient CPU utilization and low latency. As we don't need to recreate a new thread every single time we have a new task so high performance is a clear benefit in this case

    2.2.3 Case 3

    Another very typical multi-threaded software architecture used in back-end services where we have a microservice that acts as a software abstraction layer on top of a database. Every request is handled by a different thread but eventually, all requests become either reads or write from or to a database the connection to that database is represented by an object or a set of objects which are again shared by all the request threads. In this case, we absolutely have to have this shared connection as we have only one database but there are many independent requests coming in all the time.

    3. Atomic Operations

    3.1 Intuition

    • InventoryCounter is a shared object between two threads
    • items++ and items-- are happening at the same time. Those are not atomic operations.
    public class demo {
        public static void main(String[] args) throws InterruptedException {
            InventoryCounter inventoryCounter = new InventoryCounter();
            DecrementingThread decrementingThread = new DecrementingThread(inventoryCounter);
            IncrementingThread incrementingThread = new IncrementingThread(inventoryCounter);
    
            decrementingThread.start();
            incrementingThread.start();
    
            decrementingThread.join();
            incrementingThread.join();
            System.out.println(inventoryCounter.getItems());
        }
    
        public static class DecrementingThread extends Thread {
            private InventoryCounter inventoryCounter;
            public DecrementingThread(InventoryCounter inventoryCounter) {
                this.inventoryCounter = inventoryCounter;
            }
    
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    inventoryCounter.decrement();
                }
            }
        }
    
        public static class IncrementingThread extends Thread {
            private InventoryCounter inventoryCounter;
            public IncrementingThread(InventoryCounter inventoryCounter) {
                this.inventoryCounter = inventoryCounter;
            }
    
            @Override
            public void run() {
                for (int i = 0; i < 10000; i++) {
                    inventoryCounter.increment();
                }
            }
        }
    
        //Thread unsafe approach
    //    public static class InventoryCounter {
    //        private int items = 0;
    //        public void increment() { items++; }
    //        public void decrement() { items--; }
    //        public int getItems() { return items; }
    //    }
    
        // Thread safe approach using atomic variable
        public static class InventoryCounter {
            private AtomicInteger item = new AtomicInteger(0);
            public void increment() { item.incrementAndGet(); }
            public void decrement() { item.decrementAndGet(); }
            public int getItems() { return item.get(); }
        }
    }

    3.2 Atomic Operation

    • An operation or a set of operations is considered atomic, if it appears to the rest of the system as if it occurred at once.
    • Single-step which means all or nothing.
    • No intermediate states

    4. Reference

    https://en.wikipedia.org/wiki/Critical_section

    댓글

Designed by Tistory.