The difference between Linux and Windows system threads

  

Linux and Windows
system threads, people familiar with WIN32 programming must know that WIN32 process management is very different from Linux, in Unix There is only the concept of process, but there is a concept of "thread" in WIN32, then what is the difference between Linux and WIN32 here? The process/thread in WIN32 is inherited from OS/2. In WIN32, "process" refers to a program, and "thread" is an execution "thread" in a "process". From the core, WIN32's multi-process is not much different from Linux. The thread in WIN32 is equivalent to the Linux process, which is a code that is actually being executed. However, WIN32 shares data segments between threads in the same process. This is the biggest difference from the Linux process. Under WIN32, use the CreateThread function to create a thread. Unlike the creation process under Linux, the WIN32 thread does not run from the creation point. Instead, a function is specified by CreateThread, and the thread starts running from that function. This program, like the previous UNIX program, prints 1000 messages each by two threads. threadID is the thread number of the child thread. In addition, the global variable g is shared between the child thread and the parent thread, which is the biggest difference from Linux. As you can see, the process/thread of WIN32 is more complicated than Linux. It is not difficult to implement a WIN32-like thread in Linux. As long as the fork is executed, let the child process call the ThreadProc function and open a shared data area for the global variable, but Under the WIN32 can not achieve the function of similar fork. So now the library functions provided by the C compiler under WIN32 are compatible with most Linux/UNIX library functions, but they still cannot implement fork. For multitasking systems, sharing data areas is necessary, but it is also a problem that is easy to cause confusion. Under WIN32, it is easy for a programmer to forget that the data between threads is shared. After a thread has modified a variable. Another thread modified it, causing a problem with the program. But under Linux, because the variables are not shared, the programmer explicitly specifies the data to be shared, making the program clearer and more secure. As for the WIN32 "process" concept, its meaning is "application", which is equivalent to exec under UNIX.

Copyright © Windows knowledge All Rights Reserved