⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timeout_heap.txt

📁 快速开发
💻 TXT
字号:
How the timeout heap worksAs of version 1.5, the State Threads Library represents the queue ofsleeping threads using a heap data structure rather than a sortedlinked list.  This improves performance when there is a large numberof sleeping threads, since insertion into a heap takes O(log N) timewhile insertion into a sorted list takes O(N) time.  For example, inone test 1000 threads were created, each thread called st_usleep()with a random time interval, and then all the threads whereimmediately interrupted and joined before the sleeps had a chance tofinish.  The whole process was repeated 1000 times, for a total of amillion sleep queue insertions and removals.  With the old list-basedsleep queue, this test took 100 seconds; now it takes only 12 seconds.Heap data structures are typically based on dynamically resizedarrays.  However, since the existing ST code base was very nicelystructured around linking the thread objects into pointer-based listswithout the need for any auxiliary data structures, implementing theheap using a similar nodes-and-pointers based approach seemed moreappropriate for ST than introducing a separate array.Thus, the new ST timeout heap works by organizing the existing_st_thread_t objects in a balanced binary tree, just as they werepreviously organized into a doubly-linked, sorted list.  The global_ST_SLEEPQ variable, formerly a linked list head, is now simply apointer to the root of this tree, and the root node of the tree is thethread with the earliest timeout.  Each thread object has two childpointers, "left" and "right", pointing to threads with later timeouts.Each node in the tree is numbered with an integer index, correspondingto the array index in an array-based heap, and the tree is kept fullybalanced and left-adjusted at all times.  In other words, the treeconsists of any number of fully populated top levels, followed by asingle bottom level which may be partially populated, such that anyexisting nodes form a contiguous block to the left and the spaces formissing nodes form a contiguous block to the right.  For example, ifthere are nine threads waiting for a timeout, they are numbered andarranged in a tree exactly as follows:              1           /     \          2       3         / \     / \        4   5   6   7       / \      8   9Each node has either no children, only a left child, or both a leftand a right child.  Children always time out later than their parents(this is called the "heap invariant"), but when a node has twochildren, their mutual order is unspecified - the left child may timeout before or after the right child.  If a node is numbered N, itsleft child is numbered 2N, and its right child is numbered 2N+1.There is no pointer from a child to its parent; all pointers pointdownward.  Additions and deletions both work by starting at the rootand traversing the tree towards the leaves, going left or rightaccording to the binary digits forming the index of the destinationnode.  As nodes are added or deleted, existing nodes are rearranged tomaintain the heap invariant.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -