Multithreaded programming examples (combined with instances)

Source: Internet
Author: User

1.CreateThread and _beginthreadex

#pragmaOnce#include<cstdio>#include<Windows.h>#include<crtdbg.h>#include<process.h>//Child thread FunctionsDWORD WINAPI ThreadFun1 (lpvoid pM) {printf ("the thread ID number of the child thread is:%d\nhello world!\n", GetCurrentThreadID ()); return 0;}voidfun1 () {printf ("Simple Multithreaded instances! \ n"); /*CreateThread parameter Resolution 1: Thread Kernel security attribute 2: Line stacks space size 3: Thread execution function address 4: Pass to thread execution function parameter 5: Thread creation control parameter (create_ SUSPENDED) 6: Thread ID number*/HANDLE HANDLE= CreateThread (NULL,0, ThreadFun1, NULL,0, NULL);    WaitForSingleObject (handle, INFINITE); CloseHandle (handle);}//set the Count global variableintCOUNT =0;//Child thread FunctionsUnsignedint_stdcall ThreadFun2 (PVOID pM) {++COUNT; printf ("the thread ID number of the child thread is:%d,%d\nhello world!\n", GetCurrentThreadID (), COUNT); return 0;}voidfun2 () {printf ("Simple Multithreaded instances! \ n"); Const intThread_num =5;    HANDLE Handle[thread_num];  for(size_t i =0; i < Thread_num; i++) {Handle[i]= (HANDLE) _beginthreadex (NULL,0, ThreadFun2, NULL,0, NULL); } waitformultipleobjects (Thread_num, handle, TRUE, INFINITE);}intMainvoid){    //using CreateThread//fun1 (); //using _beginthreadex//recommended use because: when using standard C run-time library functions, race condition is prone to occur.//use _beginthreadex to avoid tampering with data by other threads//more reasonable explanations can refer to Win32 multithreaded programmingfun2 (); //Detecting Memory leaks_CrtDumpMemoryLeaks (); return 0;}

Where the execution of the fun2 result is: (Pretty interesting, no lock is so intuitive)

2. Atomic operation

#pragmaOnce#include<cstdio>#include<Windows.h>#include<crtdbg.h>#include<process.h>volatile LongCOUNT =0;Const intThread_num = -; unsignedint_stdcall Threadfun (lpvoid pM) {Sleep ( -); //++count;InterlockedIncrement ((Lplong) &count);//Replace with atomic lockSleep ( -); return 0;}voidfun1 () {HANDLE handle[thread_num];  for(size_t i =0; i < Thread_num; i++) {Handle[i]= (HANDLE) _beginthreadex (NULL,0, Threadfun, NULL,0, NULL);    } waitformultipleobjects (Thread_num, handle, TRUE, INFINITE); printf ("%d threads started with a record result of%d", Thread_num, COUNT);}intMain () {//test1Fun1 ();//easy to find thread boot count and Count mismatch//Detecting Memory leaks_CrtDumpMemoryLeaks (); return 0;}

The + + operation is divided into three layers at the assembly level: (1) The value is stored in the register, (2) in the Register, and (3) the value is dumped into memory by the register. This process is prone to problems.

But using atomic operations is accurate when only 50 threads are started, but when the upper limit is tuned to 500, the number of thread starts and counts is inconsistent.

Multithreaded programming examples (combined with instances)

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.