Multi-thread programming under WinCE

Source: Internet
Author: User

1. Basic Knowledge:

1) process: a dynamic execution process of a program with certain functions in a data set. A process consists of the body segment, user data segment, and system data segment to form an execution environment, which corresponds to the allocation and recovery of resources such as processors, memories, and peripherals. A process is the subject of the use of computer system resources, it is also the basic unit for the operating system to allocate resources.

2) thread: when multiple processes are concurrently executed, the overhead of process switching is large, which affects the efficiency of inter-process communication. Therefore, a smaller basic unit that can run independently-the thread is proposed. An entity of a process in a thread is the basic unit of CPU scheduling and allocation. Apart from some essential resources in the running process, the thread does not possess system resources, however, threads can share all resources of a process with other threads of the same process.

3) in traditional operating systems, processes are the basic units for resource allocation, independent scheduling, and allocation. After threads are introduced, as the basic unit for scheduling and allocation, processes are still the basic units for resources.

4) Relationship: in order for the process to complete some work, the process must occupy at least one thread. Therefore, the thread describes the execution in the process, and the thread is responsible for executing the Code contained in the address space of the process.

5) interlock function: runs in user mode. It ensures that other threads cannot access a variable when a thread accesses a variable to ensure the uniqueness of the variable value. This access method is called atomic access. For example, interlockedincrement (lplong.

6) event object: The event object runs in kernel mode and uses the wait function to wait for the required events and signals. During the waiting process, the thread is in sleep state. When a signal is received, run the kernel recovery thread. Four waiting functions are available: waitforsingleobject and waitformultipleobjects. Functions related to events: createevent, setevent, pulseevent, resetevent, and openevent.

Createevent creates an event object. Parameter 1 must be null. Parameter 2 specifies whether to manually reset the state of the event object. If it is false, when the wait function receives a signal and returns the result, the event object is automatically set to invalid. Other threads waiting for the event object will not be awakened. If the value is true, it will not be set to invalid, and other threads waiting for this event object will also be awakened.

The resetevent function can manually invalidate the event object.

The setevent function sets the event object to valid.

Openevent opens the created event object, which is generally used for Thread Synchronization in different processes.

2. Thread Programming Technology

1) Compile a thread function, which must be similar to a small prototype:

DWORD winapi mythread (lpvoid lpvthreadparm );

Note that only one parameter is allowed. This function cannot be called by users. The operating system calls an internal function, such as startofthread.

2) Creation thread: the main thread of a thread is automatically generated by the operating system. The main thread creates other threads and uses the createthread function.

3) Terminating thread: A thread calls the exitthread function and terminates itself. Calling the terminatethread function can terminate a specified thread.

4) Other functions: set the priority to setthreadpriority, suspend the thread suspendthread, and resume the thread resumethread.

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.