OS_chapter4.3_thread_190420

OS_chapter4.3_thread

4.3.1 concept of thread(线程)

leading question

  • program : draw circle & square at same time (ppt)
  • first DrawCircle() then DrawRect()
  • by order not the same

conclusion

  • process can’t concurrence(并发) actually
  • thread

Thread

OS_chapter4.3_thread_190420

  • create thread

  • Windows : CreateThread()
    OS_chapter4.3_thread_190420

    • ThreadFunction 作为函数指针指向原型函数
  • 改进程序 : 将DrawCircle() ,DrawRect() 作为参数传递给CreateThread() 而非 直接调用


单线程/多线程(single-thread / multi-thread)
OS_chapter4.3_thread_190420

4.3.2 typical status of thread

  1. 程序的多个功能需要并发运行
  • EG: video online concurrence functions: 1. 视频解码 2.音频解码 3. 网络接收
  • nitiate a thread for each function to realize their concurrence
  1. 改善窗口交互性(线程实现后台并发运行)(ppt)
  2. 改善程序结构(将function以线程封装组织程序)
  3. 多核CPU应用,充分发挥多核性能

supplement
OS_chapter4.3_thread_190420

  • afxBeginThread()是CreateThread()的一个封装(简单于CreateThread)
  • remote将thread封装在第三方进程空间中
  • Linux
    OS_chapter4.3_thread_190420
    OS_chapter4.3_thread_190420

the shortcomings of thread

  1. 程序难以调试(非顺序执行)
  2. 并发过程难以控制(互斥访问等)
  3. 线程安全问题