[Concurrency parallel]_[thread synchronization]_[pthread and Win32 Critical sections (Critical section) comparison]

Source: Internet
Author: User
Tags assert mutex posix


1. In a multithreaded program, the critical section is the most common solution for synchronizing access to shared resources.

2. Pthread is a cross-platform threading model, so what is the difference between this and the local threading model for critical section programming?

Critical section:

A critical section is a small piece of code that must monopolize access to certain resources before the code can execute. This is a way for some code to be able to use resources "in an atomic way".

The so-called Atomic (atomic) operation means that this code knows that no other thread is going to access this resource.


1. Macosx,windows has its own threading model, Pthread can be said to be a cross-platform threading programming model solution, of course, for pthread unfamiliar can also use the local threading model,

In fact, Pthread's Win32 version is also based on the local threading model, Pthread-win32 mutex is the user state of the critical area implementation, and Win32 kernel object mutex is different.

2. POSIX thread (POSIX threads), referred to as pthreads, is the POSIX standard for threading. The standard defines a complete set of APIs for creating and manipulating threads. In Unix-like operating systems (Unix, Linux, Mac OS x, etc.),

Use Pthreads as the operating system thread. The Windows operating system also has its ported version of Pthreads-win32.


3. The critical section belongs to the user-mode object, which is not entirely true. If a thread attempts to enter a critical section owned by another thread, the thread is placed in a wait state.

The only way to get into the waiting state is to use threads to move from user mode to kernel mode, so the pursuit of high-performance critical areas is to try to avoid the kernel mode in the waiting state.



Test_user_sync.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <stdint.h> #include <stdlib.h> #include <assert.h> #include < stdio.h> #include <Windows.h> #include "pthread.h" #include <iostream>using namespace std;static pthread _mutex_t Cs_mutex = null;static pthread_barrier_t barrier = null;static critical_section cs;static int32_t gCount = 0;sta Tic int32_t gexecutecount = 0;static const int kthreadnum = 10;static const int kloopnum = 100;void Sync_func () {++gexecute Count;cout << "Sync_func currentthreadid:" << getcurrentthreadid () << "Gexecutecount:" << gexe Cutecount << ENDL;//1. The counter increments//1. Asserts that only one thread accesses this code. ASSERT (Gcount = = 0); ++gcount;assert (gcount = 1);//1. Hangs 0.1 seconds, Simulation is time consuming. Sleep (100);//1. Leave function, counter decrements assert (Gcount = = 1);--gcount;assert (Gcount = = 0);} void* startpthread (void* data) {for (int i = 0; i < Kloopnum; ++i) {pthread_mutex_lock (&cs_mutex); Sync_func ();p thre Ad_mutex_unlock (&cs_mutex);//1. Suspend for 0.1 seconds, giving other threads the opportunity to execute.    Sleep (100);} Pthread_barrier_wait (&barrier); return NULL;} void Testpthreadcriticalsection () {gexecutecount = 0;pthread_mutex_init (&cs_mutex,null);//1. Create 10 threads.//1. including the current thread +1pthread_barrier_init (&barrier,null, Kthreadnum + 1); for (int i = 0; i< kthreadnum; ++i) {pthread_t t;pthread_create (&t,null,startpthread,null);p Thread_detach (t);} Pthread_mutex_destroy (&cs_mutex)//1. Wait for other threads to finish executing. pthread_barrier_wait (&barrier); Assert (kthreadnum* Kloopnum = = Gexecutecount);} DWORD WINAPI startwin32thread (PVOID pvparam) {for (int i = 0; i < Kloopnum; ++i) {entercriticalsection (&AMP;CS); sync_ Func (); LeaveCriticalSection (&cs);//1. Suspend for 0.1 seconds, giving other threads the opportunity to execute. Sleep (100);} return 0;}        void Testwin32criticalsection () {gexecutecount = 0; InitializeCriticalSection (&AMP;CS);    HANDLE Hthreads[kthreadnum]; for (int i = 0; i< kthreadnum; ++i) {DWORD Dwthreadid;hthreads[i] = CreateThread (null,0,startwin32thread,null,0,& dwThreadID);} DWORD rc = WaitForMultipleObjects (Kthreadnum, Hthreads, TRUE, INFINITE); assert (rc = = WAIT_OBJECT_0); for (int i = 0; i< kthreadnum; ++i) {CloseHandle (hthreads[i]);} DeleteCriticalSection (&AMP;CS);} int _tmain (int argc, _tchar* argv[]) {testwin32criticalsection (); System ("pause"); Testpthreadcriticalsection (); System ("pause"); return 0;}









The 4th edition of Windows core programming

[Concurrency parallel]_[thread synchronization]_[pthread and Win32 Critical sections (Critical section) comparison]

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.