Xinxin Sun VC Notes-Multithreaded programming

Source: Internet
Author: User
Tags sleep function terminates

1. A process is an instance of a running program that never executes anything, just a container of threads. For a process to complete an operation, it must have a thread running in its environment that is responsible for executing the code contained in the process address space.
2. When a process is created, the operating system automatically creates the first thread of the process, the thread that executes the main function or the WinMain function, the main function or the WinMain function is the entry point function of the main thread, after which the thread can create additional threads.
3. The system gives each process a separate virtual address space, and for a 32-bit process, the 32 power of the address space is 4GB.
4. Each process has its own private address space. Assuming that process A has a data structure that resides in its address space, the address is 0x12345678, and process B has a different data structure in its address space, the address is 0x12345678, and when the thread running in process a accesses memory with the address 0x12345678, The data structure of a is accessed, and when the thread running in process B accesses memory with the address 0x12345678, the data structure of B is accessed. Threads running in Process a cannot access data structures in the address space of process B.
5. Page files partition a space on disk as memory usage, increasing the amount of memory that is called virtual memory.
6. 4GB virtual address space, 2GB is the kernel mode partition, for the kernel code device driver and so on, and the user mode partition address space is 2GB, this partition is the private address space of the process. A process cannot read, write, or access data from another process residing in that partition in any way.
7. Thread consisting of two parts
1) Thread kernel object. When a thread is created, the system creates a thread kernel object that is not the thread itself, but rather a smaller data structure that the operating system uses to manage threads.
2) thread stack. Used to maintain all the parameters and local variables that the thread needs to execute code.
8. The thread has only one kernel object and one stack and requires little memory. Because threads require less overhead, you often use multithreading to solve problems when programming, and try to avoid creating new processes.
9. The operating system schedules a certain amount of CPU time slices for each running thread. Threads run in their own time slices, because the time slices are fairly short, so the user feels like the thread is running at the same time. When the computer has multiple CPUs, the thread can actually run at the same time.
10. In multithreaded programs, the main thread end program exits, so using the sleep function to make the main thread sleep, other threads are executed, and other threads are executed alternately.
11. When a thread executes alternately, when a thread starts executing, it executes from where it was last paused, and if multiple threads share resources and sometimes cause errors, use the thread synchronization mechanism.
12. Mutex Object
1) A mutex is a kernel object that ensures that a thread has exclusive access to a single resource.
2) A mutex object consists of a usage quantity, a thread ID, and a counter.
3) ID is used to identify which thread in the system currently has a mutex, and the counter is used to indicate the number of times that the thread has a mutex.
13. Create Mutex object function CreateMutex
1) The second parameter indicates whether the thread that created the mutex has a mutex object.
2) Because mutex objects are common, they are generally declared as global variables.
3) The third parameter specifies the mutex position, NULL is the anonymous mutex, not nul is a name mutex, when the mutex object is named, if the mutex was previously created, the mutex object is returned, the mutex object is created, and the GetLastError function is called. Determines whether the mutex is created, based on the GetLastError return value. Using this feature ensures that only one instance of the application is running.
14. Request Mutex Object function WaitForSingleObject
1) The first parameter is a handle to the requested mutex, and the function returns when the mutex has a signaled state.
2) The second parameter is time, and when this time is reached, regardless of whether the requested mutex has a signal, the function returns, set to 0 returns immediately, and set to infinite waits for the signal state of the requested object. At this point the thread calling this function pauses execution, waiting for the mutex object.
3) When there is no thread owning the mutex, the mutex object is signaled.
4) When the thread that owns the mutex executes, to release the mutex, call the ReleaseMutex function, and if the 1 counter is 0, the operating system sets the mutex object's thread ID to 0 and sets the signal state to be used by other threads.
15. Releasing the mutex function ReleaseMutex
When a mutex is released, the operating system compares the thread ID that called the function with the same thread ID that currently owns the mutex, unlike the one that does not allow deallocation, so the mutex is who owns who releases it (not who creates who releases it).
16. When a mutex object is created, the counter is set to 1 when the mutex object is requested, counter +1, and 1 is released. If a mutex is requested multiple times in one thread, it is released multiple times.
17. If a thread requests a mutex and does not release it, the operating system automatically frees the mutex object when the owning thread terminates.
By requesting the return value of the mutex, you can tell whether the requested mutex is normally freed or is released by the operating system after its owner terminates.

Xinxin Sun VC Notes-Multithreaded programming

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.