About multitasking and multithreading

Source: Internet
Author: User

Multitasking is the ability of the operating system to execute multiple programs. The operating system uses hardware clocks to configure time fragments for each program. The Windows 16-bit version supports a limited number of tasks, and the Windows 32-bit version supports true multitasking and multi-threading.

Multithreading is the implementation of "multitasking" within a program.

DOS systems are not much help for multitasking, and the major DOS versions are designed based on the capabilities of 8086 and 8088 chips, which are not designed for multitasking, partly because memory management is not strong enough. When you start and end multiple programs, the multitasking operating system needs to move memory blocks to collect free memory. However, creative programmers have found some way, mostly using resident programs, such as the background print queue program, by intercepting hardware clock interrupts to perform real background processing.

Windows 1.0 already has the ability to move memory blocks in physical memory, which is a prerequisite for multitasking, although the move method is not yet fully transparent to the application, but it is tolerable. Early Windows multitasking or non-priority multitasking occurs when the program finishes processing the message and returns control to Windows. This is also referred to as "cooperative multitasking" because it requires some cooperation from the application, a Windows program can occupy the entire system if it takes a long time to process the message.

The 32-bit version of Windows supports non-serialized Message Queuing, which avoids having an application occupy the entire system. (I don't understand that either)

In multi-threading, a program can split itself into a fragment that executes simultaneously (that is, a thread).

A thread is simply represented as a function that can call other functions in the program. The program executes from its main thread, which is a function called Main in a traditional C program, which is WinMain in Windows. Once executed, the program can create a new thread through Createthead, and the inter-thread priority switch.

A multithreaded architecture: the main thread processes the consumer input message and establishes other threads, which are only used for background processing, except communication with the main thread and not the user. Just like the boss and the staff, the boss throws the big job to the staff and keeps in touch with the outside world.

Threads share programs ' memory, so they share static variables. But they all have their own stacks, so dynamic variables are unique to each stack. Each thread also has its own processor state, which is stored and restored during a live thread switchover.

Designing a complex multithreaded application correctly is one of the most difficult tasks that Windows programmers may encounter, because priority multitasking can interrupt a thread at any moment, switching control to another thread.

A common error in multi-threading is the "race state", in order to help coordinate the activity of threads, the operating system requires various forms of synchronization, one is the synchronous signal (semaphore), which allows one thread to be prevented from executing at some point in the program code until the other threads signal to let it continue. There is also a critical region (critical section), which is a non-interruptible part of the program code.

Synchronization signals can also cause deadlocks, and two of threads prevent each other from executing.

Resources:

1, Charles Petzold. "Windows Programming"

About multitasking and multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.