Differences between Linux and Windows threads

Source: Internet
Author: User
Article Title: differences between Linux and Windows threads. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

Anyone familiar with WIN32 programming must know that the WIN32 process management method is very different from that on Linux. in Unix, there is only a process concept, however, there is a "Thread" concept in WIN32. What is the difference between Linux and WIN32?

The process/thread in WIN32 inherits from OS/2. In WIN32, "process" refers to a program, and "Thread" refers to an execution "clue" in "process ". At the core, the multi-process of WIN32 is not much different from that of Linux. The thread in WIN32 is equivalent to a Linux Process and is actually executing code. However, in WIN32, threads in the same process share data segments. This is the biggest difference with Linux processes.

The following section shows how the next WIN32 process starts a thread.

Int g;

Dword winapi ChildProcess (LPVOID lpParameter ){

Int I;

For (I = 1; I <1000; I ++ ){

G ++;

Printf ("This is Child Thread: % d \ n", g );

}

ExitThread (0 );

};

Void main ()

{

Int threadID;

Int I;

G = 0;

CreateThread (NULL, 0, ChildProcess, NULL, 0, & threadID );

For (I = 1; I <1000; I ++ ){

G ++;

Printf ("This is Parent Thread: % d \ n", g );

}

}

In WIN32, The CreateThread function is used to create a thread. Unlike the creation process in Linux, the WIN32 thread does not start to run from the creation process, but is specified by CreateThread, the thread starts to run from that function. This program is the same as the previous UNIX program, with 1000 pieces of information printed by each of the two threads. ThreadID is the thread number of the subthread. In addition, the global variable g is shared between the subthread and the parent thread. This is the biggest difference with Linux. As you can see, WIN32 processes/threads are more complex than Linux. in Linux, it is not difficult to implement threads similar to WIN32. As long as the fork is passed, the sub-process can call the ThreadProc function, in addition, you can set up a shared data zone for global variables. However, fork-like functions cannot be implemented in WIN32. Therefore, although the library functions provided by the C language compiler under WIN32 are compatible with most Linux/UNIX library functions, fork cannot be implemented yet.

For multi-task systems, sharing the data zone is necessary, but it is also a problem that is easy to cause confusion. in WIN32, a programmer can easily forget that the data between threads is shared, after a thread modifies a variable, the other thread modifies it again, causing a program issue. However, in Linux, because variables are not shared, programmers explicitly specify the data to be shared, making the program clearer and safer.

As for the WIN32 "process" concept, it means "application", which is equivalent to exec in UNIX.

 

Related Article

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.