What Are Threads (What is a Thread)?

前端 未结 5 579
鱼传尺愫
鱼传尺愫 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.

    0 讨论(0)
  • 2020-12-31 22:33

    There is a nice page on wikipedia: http://en.wikipedia.org/wiki/Thread_%28computer_science%29

    short summary: A thread is a light process

    If you use threads, pay attention to the behavior of each thread and especially to mutual ressources

    0 讨论(0)
  • 2020-12-31 22:44

    At the risk of oversimplifying:

    A thread is a line of execution through a program.

    In your basic programming model, the computer simply traces through your program one statement at a time, and at any given time, only one statement is being excecuted. If your program branches or calls another routine, execution will leave the place where control transferred and begin executing at another place, but still, only one thing is being done at any one time.

    With threads, multiple lines of control can be executing at the same time. For example, one part of your program can be interacting with the user, while another part is downloading a file in the background. Multi-threaded programs are much harder to program and it's much harder to envisage how they work.

    0 讨论(0)
  • 2020-12-31 22:45

    A simple explanation would be that you have a job to do, and you get a single person to do the job.

    This single person is similar to a thread in a computer.

    One person can do one thing at a time, so to do the job, he proceeds through the tasks of the job, one task at a time, one operation on each task at a time.

    To speed up a job, you can put more people to the same job. For instance, let's say you need to paint a house. You hire 4 people to do this.

    These 4 people could be similar to 4 threads in that they work with the same resources (same house, same buckets of paint) and they can divide up the work.

    A process would be akin to the job of painting that house.

    This simple explanation somewhat breaks down when it comes to machines that doesn't have enough CPU cores to run all the threads simultaneously, but I'm going to ignore this here.

    0 讨论(0)
  • 2020-12-31 22:45

    An individual thread is so named because it is a single thread of execution through your code. If you have multiple threads then you have multiple threads of execution through your code simultaneously (or as simultaneously as your single/multicore system supports). Both threads have access to the same heap though use different stacks. This means data in your program may be visible by both threads and may be changed by either thread. This can of course lead to serious problems which requires protection against.

    It is worth noting that a thread is different to a process. A key difference is that two threads can access the same data (heap) while two processes cannot.

    For a more full description see other online descriptions

    wikipedia

    0 讨论(0)
提交回复
热议问题