About processes and threads

xiaoxiao2021-03-06  104

The author recently encountered such a problem during the Internet-based visual phone, that is, in a visual telephone system based on the in Ternet online, speaking, voice compiling codes, image acquisition, image compiling codes, voice And the transmission of image streams, all of these work is parallel. In particular, speech signals, if the image codec is too long, the speech signal is not available, the call has an intermittent; if the image or speech processing time is too long, the code stream data cannot be transmitted in time, and the communication is also interrupted. This requires us to implement a parallel programming, on only one CPU machine, that is, to assign the CPU time to all events, regularly handle each event, and will not process a certain event . Under 32-bit Windows 95 or Windows NT, we can implement this parallel process with multi-threaded processing technology. In fact, this parallel programming is necessary under many occasions. For example, when a file file is copied, it displays a Cancel button in a dialog. If you are in the file copy, the Cancel button will terminate the copy. In the 16-bit WinOWS, this type of function needs to periodically call the PeekMessage function inside the File Copy loop. If you are reading a big action; if you read the file from the floppy disk, you have to spend a few seconds. Since the machine response is too slow, the user will frequently point this button, so that the system does not know what to terminate. If you put the File Copy instruction into another thread, you don't need to put a large pile of PeekMessage functions in your code, and process the thread of the user interface will be separated separately, and you will get a response immediately after the CANCEL button. The same reason, it is also useful to create a separate thread in the application to handle all print tasks, and users can continue using the application when printing. Thread concept In order to understand the concept of thread, we must first discuss the concept of the process. A process is typically defined as an instance of the program. In 32-bit Windows, the process occupies 4GB virtual address space. Unlike them in MS-DOS and 16-bit Windows operating systems, 32-bit Windows processes are inevitable. That is to say, a 32-bit Windows process does not perform any instructions, it just occupies 4GB address space, this space has code and data for application EXE files. The DLL required for EXE also loads the data of their code into the address space of the process. In addition to address space, the process also possesses certain resources, such as files, dynamic memory allocation, and threads. When the process is terminated, the various resources created in its lifetime will be cleared. As mentioned above, the process is not viable, it is just a static concept. In order to make the process completed some work, the process must have at least a thread, so the thread is the execution within the process, exactly the thread is responsible for executing the code included in the address space included in the process. In fact, a single process may contain several threads that can execute the code in the address space of the process. In order to do this, each thread has its own set of CPU registers and vertices. Each process has at least one line address in executing the code in its address space. If there is no thread executes the code in the process floor space, if there is no thread execute the code in the process address space, the process does not continue to exist, the system The process and its address space will be automatically cleared. In order to run all of these threads, the operating system arranges some CPU time for each standalone thread, and the operating system provides time fragments to threads in rotation, which gives people a description, as if these threads are running at the same time. When you create a 32-bit Windows process, its first thread is called the main thread, generated automatically by the system, and then generates additional threads by this main line, which can generate more threads. Thread programming technology 1. Writing thread function All threads must start from a specified function, which is called a thread function, which must have the following original: DWORD WINAPI YOTHREADFUNC (LPVOID LPVT.HREADPARM); This function enters an LPVOID type The parameters can be an integer of a DWORD type, or a pointer to a buffer, returning a DWORD type value. Like the Winmain function, this function is not called by the operating system, the operating system calls an internal function of the non-C run in the kernel32.dll, such as StartoftHread, and then build an exception handling framework by the S tartofthread function, call us The function. 2. Creating a thread a process of a process is automatically generated by the operating system. If you want a primary thread to create additional threads, you can call CreateThread to complete.

Format is as follows: HANDLE CreateThread (LPSECURITY_ATTRIBUTES jpsa.DWORD cbstack, LPTHREAD_START _ROUTINE lpStartAddr.LPVOID lpvThreadParm, DWORD fdwCreate, LPDWORD lpIDThread); wherein the parameters have the following meanings: lpsa: is a pointer SECURITY_ATTRIBUTES structure. If you want an object to be default security properties, you can pass a null; if you want to let any child process can inherit the thread object handle, you must specify a security _attributes structure, where binheritHandle is initialized to Ture. CBSTARK: Indicates the size of the address space assigned to the stack used by the thread, and 0 indicates the system default value. LPSTARTADDR: Indicates the address of the function where the new thread starts executing the code, that is, the thread function. LPVTHREADPARM: It is a parameter that passes the thread function. FDWCREATE: Specifies the additional flag created by the control thread, you can take two values. If this parameter is 0, the thread will start execution; if the parameter is CREATE_SUSPENDED, the system generates the thread, initializes the CPU, register member of the Context structure, ready to execute the first instruction in the thread function, but not Immediately, but hang the thread. LPIDTHRDAD: is a DWORD type address that returns the ID value assigned to the new thread. 3. Termination Thread If a thread calls the exitthread function, you can terminate yourself, such as: void extraad (unit fullxitcode); then terminate the thread after setting an exit code Fuexitcode for calling the function. Call the TerminateThread function or terminate the thread. Such as: Bool TerminateThread (Handle Hthread, DWORD DWEXITCODE); This function is used to end the thread specified by the HTHREAD parameter and set DWEXITCODE to the exit code of the thread. When a thread is no longer responding, we can call this function with other threads to finalize this unresponsive thread. 4. Setting the relative priority of the thread When a thread is created for the first time, its priority is equivalent to the priority of its process. In a single process, you can change the relative priority of the thread by calling the SetThreadPRITY function. A thread's priority is relative to the process priority it belongs. BOOL SetThreadPriority (HANDLE hThread, intnPriority); wherein the parameter is a handle to be modified hThread priority thread, nPriority may be the following values: THREAD_PRIORITY_LOWEST THREAD_PRIORITY_BELOW_NORMAL THREAD_PRIORITY_NORMAL THREAD_PRIONRITY_ABOVE_NORMAL THREAD_PRIONITY_HIGHEST. 5. Suspend and recovery thread previously mentioned threads that can be created, can be implemented by passing (create_suspended flags to function creming, when this is operated, the system creates the core object of the specified thread, creating a thread's stack, In the Context structure, the thread CPU registration member is initialized. However, the thread object is assigned an initial pending count value 1, which indicates that the system will no longer allocate the CPU to perform thread. To start executing a thread, another thread must call ResumeThread and The thread handle returned when it is passed to CreateThread. The format is as follows: DWord ResumeTHread (Handle Hthread); a thread can be hanging multiple times. If a thread is suspended 3 times, the thread must be Restore 3 times. In addition to using the create_suspended flag when creating a thread, you can also use the SuspendThread function to hang the thread. The format is as follows: DWORD SUSPENREAD (Handle Hthread). Application of multi-threaded programming technology is as mentioned earlier, in order to achieve TCP / IP The video call must be "parallel" to perform voice acquisition, speech codec, image acquisition, image coding, and reception and transmission of the code stream data.

转载请注明原文地址:https://www.9cbs.com/read-102887.html

New Post(0)