What Are Threads (What is a Thread)?

前端 未结 5 712
鱼传尺愫
鱼传尺愫 2020-12-31 21:48

I\'m always confused about thread concepts. I haven\'t a chance to use them in a real environment so far. It would be helpful if someone could explain threads for me.

5条回答
  •  梦谈多话
    2020-12-31 22:33

    Something no one every took the time to explain to me was the difference between a process and a thread. Once you understand that, where threads fit makes a lot of sense.

    An operating system gives a process memory to use. A process when started usually has one "thread" running within it.

    The thread is what the operating system schedules to run on the CPU and it is given an address to start executing instructions from.

    Some people who were much smarter than me worked out that making processes in most operating systems was much more expensive than creating a thread of execution. Additionally two threads in the same process have the ability to access the processes memory without using an operating system call and/or shared memory to do so, meaning that although you now need to synchronise the threads memory access, you could do more work in less time.

    Thus threading is an important concept to understand and it's main use was to increase the performance of programs which had concurrency that could be exploited, the first major use (EDIT: This may not have been the "first" use) being running the GUI of an application on one thread, and performing processing on another, the cornerstone of modern user interface design.

提交回复
热议问题