A Preliminary Study of Windows Critical Section

multithreading conflict

Multiple threads of a process can share process resources, such as code segment, heap space, data segment, open files and other resources. When multiple threads compete for shared resources, if no effective measures are taken, it will cause sharing. Data clutter.

The following code creates two threads, each of iwhich 1000 times

#include <iostream>
#include <thread>

using namespace std;

int num = 0;

void test()
{
    
    
	for (int j = 0; j < 1000; j++) {
    
    
		num++;
	}
}
int main()
{
    
    
	cout << "Start two threads." << endl;

	thread thread_test1(test);
	thread thread_test2(test);

	thread_test1.join();
	thread_test2.join();

	cout << "two thread joined." << endl;

	cout << "Now num is " << num << endl;

	return 0;
}

console output

Start two threads.
two thread joined.
Now num is 1543
Start two threads.
two thread joined.
Now num is 2000

It can be seen that the result of each operation is not necessarily2000

critical section

Because multithreaded execution of this code that operates on a critical resource can lead to a race condition, this code is called a critical section.

WindowsThe intermediate critical section involves four key functions:

#include <string>
#include <iostream>
#include <process.h>
#include <windows.h>

using namespace std;

//定义一个临界区
CRITICAL_SECTION g_cs;

int num = 0;

//线程绑定的函数返回值和参数是确定的,而且一定要__stdcall
unsigned __stdcall test(void* param)
{
    
    
    EnterCriticalSection(&g_cs);//进入临界区,如果有其他线程则等待
    for (int j = 0; j < 1000; j++) {
    
    
        num++;
    }
    cout << *(string*)(param) << endl;
    LeaveCriticalSection(&g_cs);//退出临界区,其他线程可以进来了
    return 1;
}


int main()
{
    
    
    //初始化临界区
    InitializeCriticalSection(&g_cs);

    HANDLE hth1, hth2, hth3;
    string s1 = "first", s2 = "second";

    //创建线程
    hth1 = (HANDLE)_beginthreadex(NULL, 0, test, &s1, 0, NULL);
    hth2 = (HANDLE)_beginthreadex(NULL, 0, test, &s2, 0, NULL);

    //等待子线程结束
    WaitForSingleObject(hth1, INFINITE);
    WaitForSingleObject(hth2, INFINITE);

    //一定要记得关闭线程句柄
    CloseHandle(hth1);
    CloseHandle(hth2);

    cout << "Now num is " << num << endl;

    //删除临界区
    DeleteCriticalSection(&g_cs);
}

output result

second
first
Now num is 2000

Guess you like

Origin blog.csdn.net/Star_ID/article/details/126572499