Concurrent Hash Tables: Fast and General?(!)

Total Page:16

File Type:pdf, Size:1020Kb

Concurrent Hash Tables: Fast and General?(!) Concurrent Hash Tables: Fast and General(?)! Tobias Maier1, Peter Sanders1, and Roman Dementiev2 1 Karlsruhe Institute of Technology, Karlsruhe, Germany ft.maier,[email protected] 2 Intel Deutschland GmbH [email protected] Abstract 1 Introduction Concurrent hash tables are one of the most impor- A hash table is a dynamic data structure which tant concurrent data structures which is used in stores a set of elements that are accessible by their numerous applications. Since hash table accesses key. It supports insertion, deletion, find and update can dominate the execution time of whole applica- in constant expected time. In a concurrent hash ta- tions, we need implementations that achieve good ble, multiple threads have access to the same table. speedup even in these cases. Unfortunately, cur- This allows threads to share information in a flex- rently available concurrent hashing libraries turn ible and efficient way. Therefore, concurrent hash out to be far away from this requirement in partic- tables are one of the most important concurrent ular when adaptively sized tables are necessary or data structures. See Section 4 for a more detailed contention on some elements occurs. discussion of concurrent hash table functionality. To show the ubiquity of hash tables we give a Our starting point for better performing data short list of example applications: A very sim- structures is a fast and simple lock-free concurrent ple use case is storing sparse sets of precom- hash table based on linear probing that is however puted solutions (e.g. [27], [3]). A more compli- limited to word sized key-value types and does not cated one is aggregation as it is frequently used support dynamic size adaptation. We explain how in analytical data base queries of the form SELECT to lift these limitations in a provably scalable way FROM... COUNT... GROUP BY x [25]. Such a query se- and demonstrate that dynamic growing has a per- lects rows from one or several relations and counts formance overhead comparable to the same gener- for every key x how many rows have been found alization in sequential hash tables. (similar queries work with SUM, MIN, or MAX). Hash- We perform extensive experiments comparing ing can also be used for a data-base join [5]. An- the performance of our implementations with six of other group of examples is the exploration of a large the most widely used concurrent hash tables. Ours combinatorial search space where a hash table is are considerably faster than the best algorithms used to remember the already explored elements with similar restrictions and an order of magnitude (e.g., in dynamic programming [36], itemset mining faster than the best more general tables. In some [28], a chess program, or when exploring an implic- extreme cases, the difference even approaches four itly defined graph in model checking [37]). Simi- orders of magnitude. larly, a hash table can maintain a set of cached ob- arXiv:1601.04017v2 [cs.DS] 6 Sep 2016 Category: [D.1.3] Programming Techniques jects to save I/Os [26]. Further examples are dupli- Concurrent Programming [E.1] Data Structures cate removal, storing the edge set of a sparse graph Tables [E.2] Data Storage Representation Hash- in order to support edge queries [23], maintaining table representations the set of nonempty cells in a grid-data structure used in geometry processing (e.g. [7]), or maintain- Terms: Performance, Experimentation, Mea- ing the children in tree data structures such as van surement, Design, Algorithms Emde-Boas search trees [6] or suffix trees [21]. Keywords: Concurrency, dynamic data struc- Many of these applications have in common that tures, experimental analysis, hash table, lock- { even in the sequential version of the program { freedom, transactional memory hash table accesses constitute a significant fraction 1 of the running time. Thus, it is essential to have 2 Related Work highly scalable concurrent hash tables that actually deliver significant speedups in order to parallelize This publication follows up on our previous findings these applications. Unfortunately, currently avail- about generalizing fast concurrent hash tables [18]. able general purpose concurrent hash tables do not In addition to describing how to generalize a fast offer the needed scalability (see Section 8 for con- linear probing hash table, we offer an extensive crete numbers). On the other hand, it seems to be experimental analysis comparing many concurrent folklore that a lock-free linear probing hash table hash tables from several libraries. where keys and values are machine words, which is There has been extensive previous work on con- preallocated to a bounded size, and which supports current hashing. The widely used textbook \The no true deletion operation can be implemented us- Art of Multiprocessor Programming" [12] by Her- ing atomic compare-and-swap (CAS) instructions lihy and Shavit devotes an entire chapter to concur- [36]. Find-operations can even proceed naively and rent hashing and gives an overview over previous without any write operations. In Section 4 we ex- work. However, it seems to us that a lot of previ- plain our own implementation (folklore) in detail, ous work focuses more on concepts and correctness after elaborating on some related work, and intro- but surprisingly little on scalability. For example, ducing the necessary notation (in Section 2 and most of the discussed growing mechanisms assume 3 respectively). To see the potential big perfor- that the size of the hash table is known exactly mance differences, consider an exemplary situation without a discussion that this introduces a perfor- with mostly read only access to the table and heavy mance bottleneck limiting the speedup to a con- contention for a small number of elements that are stant. Similarly, the actual migration is often done accessed again and again by all threads. folklore sequentially. actually profits from this situation because the con- Stivala et al. [36] describe a bounded concurrent tended elements are likely to be replicated into lo- linear probing hash table specialized for dynamic cal caches. On the other hand, any implementa- programming that only support insert and find. tion that needs locks or CAS instructions for find- Their insert operation starts from scratch when the operations, will become much slower than the se- CAS fails which seems suboptimal in the presence quential code on current machines. The purpose of of contention. An interesting point is that they our paper is to document and explain performance need only word size CAS instructions at the price differences, and, more importantly, to explore to of reserving a special empty value. This technique what extent we can make folklore more general with could also be adapted to port our code to machines an acceptable deterioration in performance. without 128-bit CAS. Kim and Kim [14] compare this table with a cache-optimized lockless implementation of hashing These generalizations are discussed in Section 5. with chaining and with hopscotch hashing [13]. The We explain how to grow (and shrink) such a table, experiments use only uniformly distributed keys, and how to support deletions and more general data i.e., there is little contention. Both linear prob- types. In Section 6 we explain how hardware trans- ing and hashing with chaining perform well in that actional memory can be used to speed up insertions case. The evaluation of find-performance is a bit and updates and how it may help to handle more inconclusive: chaining wins but using more space general data types. than linear probing. Moreover it is not specified whether this is for successful (use key of inserted After describing implementation details in Sec- elements) or mostly unsuccessful (generate fresh tion 7, Section 8 experimentally compares our hash keys) accesses. We suspect that varying these pa- tables with six of the most widely used concurrent rameters could reverse the result. hash tables for microbenchmarks including inser- Gao et al. [10] present a theoretical dynamic lin- tion, finding, and aggregating data. We look at ear probing hash table, that is lock-free. The main both uniformly distributed and skewed input dis- contribution is a formal correctness proof. Not all tributions. Section 9 summarizes the results and details of the algorithm or even an implementation discusses possible lines of future research. is given. There is also no analysis of the complexity 2 of the growing procedure. 3 Preliminaries Shun and Blelloch [34] propose phase concurrent hash tables which are allowed to use only a sin- We assume that each application thread has its own gle operation within a globally synchronized phase. designated hardware thread or processing core and They show how phase concurrency helps to im- denote the number of these threads with p. A data plement some operations more efficiently and even structure is non-blocking if no blocked thread cur- deterministically in a linear probing context. For rently accessing this data structure can block an example, deletions can adapt the approach from operation on the data structure by another thread. [15] and rearrange elements. This is not possible A data structure is lock-free if it is non-blocking in a general hash table since this might cause find- and guarantees global progress, i.e., there must al- operations to report false negatives. They also out- ways be at least one thread finishing its operation line an elegant growing mechanism albeit without in a finite number of steps. implementing it and without filling in all the detail like how to initialize newly allocated tables. They Hash Tables store a set of hKey; Valuei pairs (ele- propose to trigger a growing operation when any ments).1 A hash function h maps each key to a cell operation has to scan more than k log n elements of a table (an array).
Recommended publications
  • Algorithmic Improvements for Fast Concurrent Cuckoo Hashing Presentation by Nir David
    Algorithmic Improvements for Fast Concurrent Cuckoo Hashing Presentation by Nir David Xiaozhou Li , David G. Andersen , Michael Kaminsky , Michael J. Freedman Overview Background and Related Work Hash Tables Concurrency Control Mechanisms Naive use of concurrency control fails Principles to Improve Concurrency Concurrent Cuckoo Hashing Cuckoo Hashing Prior Work in Concurrent Cuckoo Algorithmic Optimizations Fine-grained Locking Optimizing for Intel TSX Evaluation Concurrent hash table Provides: Lookup, Insert and Delete operations. On Lookup, a value is returned for the given key, or “does not exist” if the key cannot be found. On Insert, the hash table returns success, or an error code to indicate whether the hash table is full or the key is already exists. Delete simply removes the key’s entry from the hash table. Several definitions before we go further Open Addressing: a method for handling collisions. A collision is resolved by probing, or searching through alternate locations in the array until either the target record is found, or an unused array slot is found. Linear probing - in which the interval between probes is fixed — often at 1 Quadratic probing - in which the interval between probes increases linearly Linear Probing: f(i) = i Insert(k,x) // assume unique keys 1. index = hash(key) % table_size; 2. if (table[index]== NULL) table[index]= new key_value_pair(key, x); 3. Else { • index++; • index = index % table_size; • goto 2; } 89 mod 10 = 9 Linear Probing Example 18 mod 10 = 8 Insert 89, 18, 49, 58, 69 58 mod 10 = 8 49 mod 10 = 9 Several definitions before we go further Chaining: another possible way to resolve collisions.
    [Show full text]
  • A Fast Concurrent and Resizable Robin Hood Hash Table Endrias
    A Fast Concurrent and Resizable Robin Hood Hash Table by Endrias Kahssay Submitted to the Department of Electrical Engineering and Computer Science in partial fulfillment of the requirements for the degree of Master of Engineering in Electrical Engineering and Computer Science at the MASSACHUSETTS INSTITUTE OF TECHNOLOGY February 2021 © Massachusetts Institute of Technology 2021. All rights reserved. Author................................................................ Department of Electrical Engineering and Computer Science Jan 29, 2021 Certified by. Julian Shun Assistant Professor Thesis Supervisor Accepted by . Katrina LaCurts Chair, Master of Engineering Thesis Committee 2 A Fast Concurrent and Resizable Robin Hood Hash Table by Endrias Kahssay Submitted to the Department of Electrical Engineering and Computer Science on Jan 29, 2021, in partial fulfillment of the requirements for the degree of Master of Engineering in Electrical Engineering and Computer Science Abstract Concurrent hash tables are among the most important primitives in concurrent pro- gramming and have been extensively studied in the literature. Robin Hood hashing is a variant of linear probing that moves around keys to reduce probe distances. It has been used to develop state of the art serial hash tables. However, there is only one existing previous work on a concurrent Robin Hood table. The difficulty in making Robin Hood concurrent lies in the potential for large memory reorganization by the different table operations. This thesis presents Bolt, a concurrent resizable Robin Hood hash table engineered for high performance. Bolt treads an intricate balance between an atomic fast path and a locking slow path to facilitate concurrency. It uses a novel scheme to interleave the two without compromising correctness in the concur- rent setting.
    [Show full text]
  • Software II: Principles of Programming Languages
    Software II: Principles of Programming Languages Lecture 6 – Data Types Some Basic Definitions • A data type defines a collection of data objects and a set of predefined operations on those objects • A descriptor is the collection of the attributes of a variable • An object represents an instance of a user- defined (abstract data) type • One design issue for all data types: What operations are defined and how are they specified? Primitive Data Types • Almost all programming languages provide a set of primitive data types • Primitive data types: Those not defined in terms of other data types • Some primitive data types are merely reflections of the hardware • Others require only a little non-hardware support for their implementation The Integer Data Type • Almost always an exact reflection of the hardware so the mapping is trivial • There may be as many as eight different integer types in a language • Java’s signed integer sizes: byte , short , int , long The Floating Point Data Type • Model real numbers, but only as approximations • Languages for scientific use support at least two floating-point types (e.g., float and double ; sometimes more • Usually exactly like the hardware, but not always • IEEE Floating-Point Standard 754 Complex Data Type • Some languages support a complex type, e.g., C99, Fortran, and Python • Each value consists of two floats, the real part and the imaginary part • Literal form real component – (in Fortran: (7, 3) imaginary – (in Python): (7 + 3j) component The Decimal Data Type • For business applications (money)
    [Show full text]
  • Intel Threading Building Blocks
    Praise for Intel Threading Building Blocks “The Age of Serial Computing is over. With the advent of multi-core processors, parallel- computing technology that was once relegated to universities and research labs is now emerging as mainstream. Intel Threading Building Blocks updates and greatly expands the ‘work-stealing’ technology pioneered by the MIT Cilk system of 15 years ago, providing a modern industrial-strength C++ library for concurrent programming. “Not only does this book offer an excellent introduction to the library, it furnishes novices and experts alike with a clear and accessible discussion of the complexities of concurrency.” — Charles E. Leiserson, MIT Computer Science and Artificial Intelligence Laboratory “We used to say make it right, then make it fast. We can’t do that anymore. TBB lets us design for correctness and speed up front for Maya. This book shows you how to extract the most benefit from using TBB in your code.” — Martin Watt, Senior Software Engineer, Autodesk “TBB promises to change how parallel programming is done in C++. This book will be extremely useful to any C++ programmer. With this book, James achieves two important goals: • Presents an excellent introduction to parallel programming, illustrating the most com- mon parallel programming patterns and the forces governing their use. • Documents the Threading Building Blocks C++ library—a library that provides generic algorithms for these patterns. “TBB incorporates many of the best ideas that researchers in object-oriented parallel computing developed in the last two decades.” — Marc Snir, Head of the Computer Science Department, University of Illinois at Urbana-Champaign “This book was my first introduction to Intel Threading Building Blocks.
    [Show full text]
  • Cuckoo Hashing
    Cuckoo Hashing Outline for Today ● Towards Perfect Hashing ● Reducing worst-case bounds ● Cuckoo Hashing ● Hashing with worst-case O(1) lookups. ● The Cuckoo Graph ● A framework for analyzing cuckoo hashing. ● Analysis of Cuckoo Hashing ● Just how fast is cuckoo hashing? Perfect Hashing Collision Resolution ● Last time, we mentioned three general strategies for resolving hash collisions: ● Closed addressing: Store all colliding elements in an auxiliary data structure like a linked list or BST. ● Open addressing: Allow elements to overflow out of their target bucket and into other spaces. ● Perfect hashing: Choose a hash function with no collisions. ● We have not spoken on this last topic yet. Why Perfect Hashing is Hard ● The expected cost of a lookup in a chained hash table is O(1 + α) for any load factor α. ● For any fixed load factor α, the expected cost of a lookup in linear probing is O(1), where the constant depends on α. ● However, the expected cost of a lookup in these tables is not the same as the expected worst-case cost of a lookup in these tables. Expected Worst-Case Bounds ● Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in a linear probing hash table is Ω(log n). ● Theorem: Assuming truly random hash functions, the expected worst-case cost of a lookup in a chained hash table is Θ(log n / log log n). ● Proofs: Exercise 11-1 and 11-2 from CLRS. ☺ Perfect Hashing ● A perfect hash table is one where lookups take worst-case time O(1). ● There's a pretty sizable gap between the expected worst-case bounds from chaining and linear probing – and that's on expected worst-case, not worst-case.
    [Show full text]
  • Cuckoo Hashing
    Cuckoo Hashing Rasmus Pagh* BRICSy, Department of Computer Science, Aarhus University Ny Munkegade Bldg. 540, DK{8000 A˚rhus C, Denmark. E-mail: [email protected] and Flemming Friche Rodlerz ON-AIR A/S, Digtervejen 9, 9200 Aalborg SV, Denmark. E-mail: ff[email protected] We present a simple dictionary with worst case constant lookup time, equal- ing the theoretical performance of the classic dynamic perfect hashing scheme of Dietzfelbinger et al. (Dynamic perfect hashing: Upper and lower bounds. SIAM J. Comput., 23(4):738{761, 1994). The space usage is similar to that of binary search trees, i.e., three words per key on average. Besides being conceptually much simpler than previous dynamic dictionaries with worst case constant lookup time, our data structure is interesting in that it does not use perfect hashing, but rather a variant of open addressing where keys can be moved back in their probe sequences. An implementation inspired by our algorithm, but using weaker hash func- tions, is found to be quite practical. It is competitive with the best known dictionaries having an average case (but no nontrivial worst case) guarantee. Key Words: data structures, dictionaries, information retrieval, searching, hash- ing, experiments * Partially supported by the Future and Emerging Technologies programme of the EU under contract number IST-1999-14186 (ALCOM-FT). This work was initiated while visiting Stanford University. y Basic Research in Computer Science (www.brics.dk), funded by the Danish National Research Foundation. z This work was done while at Aarhus University. 1 2 PAGH AND RODLER 1. INTRODUCTION The dictionary data structure is ubiquitous in computer science.
    [Show full text]
  • 16 Concurrent Hash Tables: Fast and General(?)!
    Concurrent Hash Tables: Fast and General(?)! TOBIAS MAIER and PETER SANDERS, Karlsruhe Institute of Technology, Germany ROMAN DEMENTIEV, Intel Deutschland GmbH, Germany Concurrent hash tables are one of the most important concurrent data structures, which are used in numer- ous applications. For some applications, it is common that hash table accesses dominate the execution time. To efficiently solve these problems in parallel, we need implementations that achieve speedups inhighly concurrent scenarios. Unfortunately, currently available concurrent hashing libraries are far away from this requirement, in particular, when adaptively sized tables are necessary or contention on some elements occurs. Our starting point for better performing data structures is a fast and simple lock-free concurrent hash table based on linear probing that is, however, limited to word-sized key-value types and does not support 16 dynamic size adaptation. We explain how to lift these limitations in a provably scalable way and demonstrate that dynamic growing has a performance overhead comparable to the same generalization in sequential hash tables. We perform extensive experiments comparing the performance of our implementations with six of the most widely used concurrent hash tables. Ours are considerably faster than the best algorithms with similar restrictions and an order of magnitude faster than the best more general tables. In some extreme cases, the difference even approaches four orders of magnitude. All our implementations discussed in this publication
    [Show full text]
  • Incremental Edge Orientation in Forests Michael A
    Incremental Edge Orientation in Forests Michael A. Bender Stony Brook University [email protected] Tsvi Kopelowitz Bar-Ilan University [email protected] William Kuszmaul Massachusetts Institute of Technology [email protected] Ely Porat Bar-Ilan University [email protected] Clifford Stein Columbia University cliff@ieor.columbia.edu Abstract For any forest G = (V, E) it is possible to orient the edges E so that no vertex in V has out-degree greater than 1. This paper considers the incremental edge-orientation problem, in which the edges E arrive over time and the algorithm must maintain a low-out-degree edge orientation at all times. We give an algorithm that maintains a maximum out-degree of 3 while flipping at most O(log log n) edge orientations per edge insertion, with high probability in n. The algorithm requires worst-case time O(log n log log n) per insertion, and takes amortized time O(1). The previous state of the art required up to O(log n/ log log n) edge flips per insertion. We then apply our edge-orientation results to the problem of dynamic Cuckoo hashing. The problem of designing simple families H of hash functions that are compatible with Cuckoo hashing has received extensive attention. These families H are known to satisfy static guarantees, but do not come typically with dynamic guarantees for the running time of inserts and deletes. We show how to transform static guarantees (for 1-associativity) into near-state-of-the-art dynamic guarantees (for O(1)-associativity) in a black-box fashion.
    [Show full text]
  • Vivid Cuckoo Hash: Fast Cuckoo Table Building in SIMD
    ViViD Cuckoo Hash: Fast Cuckoo Table Building in SIMD Flaviene Scheidt de Cristo, Eduardo Cunha de Almeida, Marco Antonio Zanata Alves 1Informatics Department – Federal Univeristy of Parana´ (UFPR) – Curitiba – PR – Brazil ffscristo,eduardo,[email protected] Abstract. Hash Tables play a lead role in modern databases systems during the execution of joins, grouping, indexing, removal of duplicates, and accelerating point queries. In this paper, we focus on Cuckoo Hash, a technique to deal with collisions guaranteeing that data is retrieved with at most two memory ac- cess in the worst case. However, building the Cuckoo Table with the current scalar methods is inefficient when treating the eviction of the colliding keys. We propose a Vertically Vectorized data-dependent method to build Cuckoo Ta- bles - ViViD Cuckoo Hash. Our method exploits data parallelism with AVX-512 SIMD instructions and transforms control dependencies into data dependencies to make the build process faster with an overall reduction in response time by 90% compared to the scalar Cuckoo Hash. 1. Introduction The usage of hash tables in the execution of joins, grouping, indexing, and removal of duplicates is a widespread technique on modern database systems. In the particular case of joins, hash tables do not require nested loops and sorting, dismissing the need to execute multiple full scans over the same relation (table). However, a hash table is as good as its strategy to avoid or deal with collisions. Cuckoo Hash [Pagh and Rodler 2004] stands among the most efficient ways of dealing with collisions. Using open addressing, it does not use additional structures and pointers - as Chained Hash [Cormen et al.
    [Show full text]
  • Cuckoo Hashing and Cuckoo Filters
    Cuckoo Hashing and Cuckoo Filters Noah Fleming May 17, 2018 1 Preliminaries A dictionary is an abstract data type that stores a collection of elements, located by their key. It supports operations: insert, delete and lookup. Implementations of dictionaries vary by the ways their keys are stored and retrieved. A hash table is a particular implementation of a dictionary that allows for expected constant-time operations. Indeed, in many cases, hash tables turn out to be on average more efficient than other dictionary implementations, such as search trees, both in terms of space and runtime. A hash table is comprised of a table T whose entries are called buckets. To insert a (key, value) pair, whose key is x, into the table, a hash function is used to select which bucket to store x. That is, a hash function is a map h : U! [T ] on a universe U mapping keys to locations in T . We will be particularly interested in the following strong family of universal hash functions. k-Universal Hash Functions: A family H of hash functions h : U! [T ] is k-universal if for any k distinct elements x1; : : : ; xk 2 U, and any outcome y1; : : : ; yk 2 [T ] k Pr [h(x1) = y1; : : : ; h(xk) = yk] ≤ 1=jT j ; h2H where the probability is taken as uniform over h 2 H. Typically, the universe U to which our keys belong is much larger than the size of our table T , and so collisions in the hash table are often unavoidable. Therefore, the hash func- tion attempts to minimize the number of collisions because collisions lead to longer insertion time, or even insertions failing.
    [Show full text]
  • Mitigating Asymmetric Read and Write Costs in Cuckoo Hashing for Storage Systems
    Mitigating Asymmetric Read and Write Costs in Cuckoo Hashing for Storage Systems Yuanyuan Sun, Yu Hua, Zhangyu Chen, and Yuncheng Guo, Huazhong University of Science and Technology https://www.usenix.org/conference/atc19/presentation/sun This paper is included in the Proceedings of the 2019 USENIX Annual Technical Conference. July 10–12, 2019 • Renton, WA, USA ISBN 978-1-939133-03-8 Open access to the Proceedings of the 2019 USENIX Annual Technical Conference is sponsored by USENIX. Mitigating Asymmetric Read and Write Costs in Cuckoo Hashing for Storage Systems Yuanyuan Sun, Yu Hua*, Zhangyu Chen, Yuncheng Guo Wuhan National Laboratory for Optoelectronics, School of Computer Huazhong University of Science and Technology *Corresponding Author: Yu Hua ([email protected]) Abstract The explosion of data volume leads to nontrivial challenge on storage systems, especially on the support for query ser- In storage systems, cuckoo hash tables have been widely vices [8, 12, 49]. Moreover, write-heavy workloads further used to support fast query services. For a read, the cuckoo exacerbate the storage performance. Much attention has hashing delivers real-time access with O(1) lookup com- been paid to alleviate the pressure on storage systems, which plexity via open-addressing approach. For a write, most demands the support of low-latency and high-throughput concurrent cuckoo hash tables fail to efficiently address the queries, such as top-k query processing [30, 33], optimizing problem of endless loops during item insertion due to the big data queries via automated program reasoning [42], of- essential property of hash collisions. The asymmetric fea- fering practical private queries on public data [47], and opti- ture of cuckoo hashing exhibits fast-read-slow-write perfor- mizing search performance within memory hierarchy [9].
    [Show full text]
  • Locks, Lock Based Data Structures  Review: Proportional Share Scheduler – Ch
    TCSS 422 A – Spring 2018 4/24/2018 Institute of Technology TCSS 422: OPERATING SYSTEMS OBJECTIVES Quiz 2 – Scheduling Review Three Easy Pieces: Assignment 1 – MASH Shell Locks, Lock Based Data Structures Review: Proportional Share Scheduler – Ch. 9 Review: Concurrency: Introduction – Ch. 26 Review: Linux Thread API – Ch. 27 Wes J. Lloyd Locks – Ch. 28 Institute of Technology Lock Based Data Structures – Ch. 29 University of Washington - Tacoma TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 23, 2018 April 23, 2018 L5.2 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma LOCKS Ensure critical section(s) are executed atomically-as a unit . Only one thread is allowed to execute a critical section at any given time . Ensures the code snippets are “mutually exclusive” CHAPTER 28 – Protect a global counter: LOCKS A “critical section”: TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 23, 2018 April 23, 2018 L7.4 Institute of Technology, University of Washington - Tacoma L5.3 Institute of Technology, University of Washington - Tacoma LOCKS - 2 LOCKS - 3 Lock variables are called “MUTEX” pthread_mutex_lock(&lock) . Short for mutual exclusion (that’s what they guarantee) . Try to acquire lock . If lock is free, calling thread will acquire the lock Lock variables store the state of the lock . Thread with lock enters critical section . Thread “owns” the lock States . Locked (acquired or held) No other thread can acquire the lock before the owner . Unlocked (available or free) releases it. Only 1 thread can hold a lock TCSS422: Operating Systems [Spring 2018] TCSS422: Operating Systems [Spring 2018] April 23, 2018 L7.5 April 23, 2018 L7.6 Institute of Technology, University of Washington - Tacoma Institute of Technology, University of Washington - Tacoma Slides by Wes J.
    [Show full text]