Windows thread synchronization (not finished)

Source: Internet
Author: User

Let's start with an API for creating threads, refer to: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx

Creates a thread to execute within the virtual address space of the the calling process.

CreateThread (  _in_opt_  lpsecurity_attributes  lpthreadattributes,  _in_      size_t                 dwstacksize,  _in_      lpthread_start_routine lpstartaddress,  _in_opt_  lpvoid                 lpparameter,  _in_      DWORD                  dwcreationflags,  _out_opt_ lpdword                lpthreadid);

Use the example reference msdn:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682516%28v=vs.85%29.aspx

For example, the thread:

#include <iostream>#include<windows.h>using namespacestd;#defineMax_threads 20#defineBuf_size 255DWORD Dwaccessorder=0;D word WINAPI mythreadfunction (lpvoid lppara) {DWORD*dwpara = (dword*) Lppara; cout<<"This is the first"<< *dwpara <<"a thread that starts up! "<<Endl; return 0;}intMain () {DWORD dwthreadidarray[max_threads];    HANDLE Hthreadarray[max_threads]; LPVOID Lpparam=nullptr;  for(inti =0; i < max_threads; i++) {Lpparam= &i;//The following conditions may occur: parameter has not been passed to the thread, it has already put the thread up, such as i=12, up a thread //The thread has not received the parameter 12 o'clock, entered the next loop, I was overwritten, was assigned to 13, so this time again//up a thread, it might be the same thread as the one that just got the parameter.//And then the thread that receives the parameter, it is possible to output to the window than the thread that receives the parameter firstHthreadarray[i] =CreateThread (NULL,0, MyThreadFunction, Lpparam,0,                                   &Dwthreadidarray[i]); } cout<<"This is the main thread"<<Endl;  for(inti =0; i < max_threads; i++) {CloseHandle (hthreadarray[i]); } System ("Pause"); return 0;}

Output, no order:

From This we can determine that the threads created and the main thread that created them are independent actuators. When they are created "quickly", they begin to "act independently" and "scramble" for the CPU. In fact, the thread is determined by the operating system according to a certain scheduling algorithm to determine who first who executes after. Multithreading is actually asynchronous, and its purpose is to quickly and alternately get the execution of the CPU, allowing the user to assume that these threads are executed in parallel .

Let's make a slight change to the example above:

#include <iostream>#include<windows.h>using namespacestd;#defineMax_threads 20#defineBuf_size 255DWORD Dwaccessorder=0;D word WINAPI mythreadfunction (lpvoid lpparam) {Dwaccessorder++; cout<<"This is the first"<< Dwaccessorder <<"a thread that starts up! "<<Endl; return 0;}intMain () {DWORD dwthreadidarray[max_threads];    HANDLE Hthreadarray[max_threads]; LPVOID Lpparam=nullptr;  for(inti =0; i < max_threads; i++) {Hthreadarray[i]=CreateThread (NULL,0, MyThreadFunction, NULL,0,                                   &Dwthreadidarray[i]); }      for(inti =0; i < max_threads; i++) {CloseHandle (hthreadarray[i]); } System ("Pause"); return 0;}

The following results are output:

When the thread T1 and T2 both need to access the variable dwaccessorder, if T1 is modifying the value of Dwaccessorder but has not yet completed, the operating system switches the execution to T2 at this point, T2 is given an incorrect value to read Dwaccessorder. Therefore, we must use some means to enable data that needs to be shared by multiple threads to be accessed by only one thread at a time. This means, called thread synchronization .

Windows thread synchronization (not finished)

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.