One of multithreaded programming--Questions raised

Source: Internet
Author: User
Tags execution terminates thread

First, the question of the proposed

Write a time-consuming single-threaded program:

Create a new dialog based application Singlethread, add a button in the main dialog idd_singlethread_dialog, ID idc_sleep_six_second, titled "6 Seconds Delay", add the response function of the button, The code is as follows:

void CSingleThreadDlg::OnSleepSixSecond()
{
  Sleep(6000); //延时6秒
}

Compile and run the application, click the "Delay 6 seconds" button, and you will notice that during the 6 seconds The program is like a "panic" and is not responding to other messages. To better handle this time-consuming operation, it is necessary to learn-multithreaded programming.

Second, multithreading overview

Both processes and threads are concepts of the operating system. A process is an instance of an application that consists of a private virtual address space, code, data, and various other system resources, the resources created by the process during the run are destroyed as the process terminates, and the system resources used are released or closed when the process terminates.

A thread is an execution unit within a process. After the system has created the process, it actually starts the main execution thread that executes the process, and the main execution thread supplies the program's startup point to the Windows system in the form of a function address, such as main or WinMain functions. The main execution thread terminates and the process terminates.

Each process has at least one main thread of execution, which is created automatically by the system without the user being actively creating it. Users create additional threads in the application as needed, and multiple threads run concurrently in the same process. All threads in a process use these virtual address spaces, global variables, and system resources in the virtual address space of the process, so the communication between threads is very convenient and the application of multithreading technology is more extensive.

Multithreading enables parallel processing, avoiding a task that takes up CPU time for a long time. The point to be explained is that most computers today are single processor (CPU), in order to run all of these threads, the operating system for each independent thread to arrange some CPU time, the operating system to rotate the thread to provide time slices, which gives a false impression, as if these threads are running at the same time. Thus, if two very active threads in order to seize control of the CPU, the thread switch will consume a lot of CPU resources, but will reduce the performance of the system. This should be noted in multithreaded programming.

WIN32 SDK functions support multithreaded programming, and provide the operating system principles of various synchronization, mutual exclusion and critical areas such as operations. In Visual C + + 6.0, the MFC class Library is also implemented in multithreaded programming, making multithreading more convenient.

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.