COMP 3430 Operating Systems
Threads part 2
February 4th, 2022
Copyright By PowCoder代写 加微信 powcoder
By the end of today’s lecture (and readings), you should be able to:
▶ Choose and use appropriate strategies for reducing costs associated with threads.
The Rocket Book (Public Domain)
It’s a pool of water ( ©rivalius13 CC BY-NC-SA 2.0)
▶ Idea: Amortize the costs
▶ Try to minimize cost of creation over
work done.
▶ Create many threads when the program
▶ Hand out already created threads when
work needs to be done.
▶ COMP 3350: Thread pools are a design pattern.
▶ More generally: Resource pools (connections, handles, etc)
▶ Let’s look at some code.
An interface! ( ©Pittigrilli and Zinnmann CC BY-SA 4.0)
Let’s take a tour of thr_pool.h:
▶ What is in the interface of this thread
▶ What operations does the thread pool
provide in terms of manipulating
▶ In terms of manipulating a thread pool?
Creating threads
Red thread. (Pixabay License)
Look at the interface and implementation of thr_pool_create() (line 260 in thr_pool.c).
▶ Design: What kinds of properties does the thread pool object have?
▶ Why are those properties hidden from client code? (don’t spend a lot of time on this!)
▶ Implementation: What does the thread pool do when it’s created?
▶ How many threads does it allocate?
▶ How does it use locks?
Starting work
Look at thr_pool_queue() (line 317 in thr_pool.c).
Just getting started. (Pixabay License)
How is work queued? How does work start?
How is this different from just creating a thread (i.e., you calling pthread_create)?
Are threads created every time work is queued?
Summarizing Pools
Stacks of threads (Pixabay License)
▶ Threads are cheap – but not free.
▶ Threadpooling is a design pattern to help
▶ Long-term costs of thread creation.
▶ Cost for creating many, short-lived task
Let’s take a look at the schedule.
It’s a clock. (Public Domain)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com