thread - anantakul · needs to seamlessly resume execution, including the thread's set of cpu...

12

Upload: others

Post on 21-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 2: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

Threads and Threading

Operating systems use processes to separate the different applications that they are executing. Threads are the basic unit to which an operating system allocates processor time, and more than one thread can be executing code inside that process. Each thread maintains exception handlers, a scheduling priority, and a set of structures the system uses to save the thread context until it is scheduled. The thread context includes all the information the thread needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process.

2

Page 3: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

Multitasking & Multithreading

Windows

Word Games Browser

Process1 Process2 Process3

Windows

Word Game Browser

Process1 Process2 Process3

thread1 thread2 thread3

Page 4: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

การท างานหลายงานพร้อมกนั

Page 5: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

Thread in Multithreading

Page 6: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

วฏัจกัรของเธรด (Thread Life Cycle)

Page 7: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

Start() and Sleep()

• Start() สัง่ให้ thread เร่ิมท ำงำนและเข้ำสูส่ถำนะ ready

• Sleep() ให้ thread พกัเข้ำสูส่ถำนะ Sleeping

Page 8: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

Join() and Stop()

• Join() ก ำหนดให้ thread ลกูท ำงำนให้เสร็จก่อน

• Stop() ก ำหนดให้ thread หยดุท ำงำน

Page 9: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

Example 1: Creating, starting, and interacting between threads

9

เร่ิมท ำงำน

เช่ือมต่อกำรท ำงำน

หยดุพกั

Page 10: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

Example 2: Creating join between threads

10

Page 11: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

Example 3: Creating threads in Application Form

11

Page 12: Thread - anantakul · needs to seamlessly resume execution, including the thread's set of CPU registers and stack, in the address space of the thread's host process. 2. Multitasking

suspend() and resume()

• suspend() สัง่ให้ thread หยดุท ำงำนและเข้ำสูส่ถำนะ blocked

• resume() ให้ thread เข้ำสูส่ถำนะ ready