4种线程同步

目录:

线程不安全例子

临界区

   CRITICAL_SECTION Critical;

初始化 InitializeCriticalSection(&Critical);

进线程之前 EnterCriticalSection(&Critical);

离开线程 LeaveCriticalSection(&Critical);

事件

   HANDLE hEvent=CreateEvent(NULL, FALSE, TRUE, L"event")), 1) ;  //自动

//其他进程要用到本事件的话不用CreateEvent,直接根据名字OpenEvent

进线程之前 WaitForSingleObject(hEvent, INFINITE);

离开线程  SetEvent(hEvent);

信号量

   HANDLE hSemaphore= CreateSemaphore(NULL, 1, 100, L"sema"), 1);   //最大资源100

     //其他进程要用到本信号量的话不用CreateSemaphore,直接根据名字OpenSemaphore

   进线程之前 WaitForSingleObject(hSemaphore, INFINITE);

   long t;

离开线程 ReleaseSemaphore(hSemaphore, 1, &t);//释放1个,释放前是t个。

互斥体

HANDLE hMutex = CreateMutex(NULL, false, L"mutex");

//其他进程要用到本互斥体的话不用CreateMutex,直接根据名字OpenMutex

进线程之前 WaitForSingleObject(hMutex, INFINITE);

         离开线程 ReleaseMutex(hMutex);

 

Int count = 0;

void getCount() {

       count ++;

       System.out.println(count);

   }

可能出现线程不安全的情况: 1 2 3 3

 

void threadMethod(int j) {

    int i = 1;

    j = j + i;

}

线程安全,因为线程之间没有共享的数据

 

 

线程不安全例子

#include <iostream>

#include <windows.h>

int count = 0;

unsigned long __stdcall PrintCount1(void*)

{

         while (count < 20)

         {

                   count++;

                   std::cout << "线程1 " << count << "\n";

                   Sleep(100);

         }

         return 1;

}

unsigned long __stdcall PrintCount2(void*)

{

         while (count < 20)

         {

                   count++;

                   std::cout <<"线程2 "<< count << "\n";

                   Sleep(100);

         }

         return 2;

}

int main()

{

         CreateThread(NULL, 0, PrintCount1, NULL, 0, NULL);

         CreateThread(NULL, 0, PrintCount2, NULL, 0, NULL);

         Sleep(10 * 1000);

         return 0;

}

/*

线程2 线程1 2

2

线程2 线程1 44

 

线程2 线程1 66

 

线程2 线程1 88

 

线程1 线程2 1010

 

线程2 线程1 1212

 

线程1 线程2 14

14

线程2 线程1 16

16

线程2 17

线程1 18

线程2 19

线程1 20*/

 

临界区

#include <iostream>

#include <windows.h>

int count = 0;

CRITICAL_SECTION Critical;

unsigned long __stdcall PrintCount1(void*)

{

         while (count < 20)

         {

                   EnterCriticalSection(&Critical);

                   count++;

                   std::cout << "线程1 " << count << "\n";

                   Sleep(100);

                   LeaveCriticalSection(&Critical);

         }

         return 1;

}

unsigned long __stdcall PrintCount2(void*)

{

         while (count < 20)

         {

                   EnterCriticalSection(&Critical);

                   count++;

                   std::cout <<"线程2 "<< count << "\n";

                  Sleep(100);

                   LeaveCriticalSection(&Critical);

         }

         return 2;

}

int main()

{

         InitializeCriticalSection(&Critical);

         CreateThread(NULL, 0, PrintCount1, NULL, 0, NULL);

         CreateThread(NULL, 0, PrintCount2, NULL, 0, NULL);

         Sleep(10 * 1000);

         return 0;

}

/*

线程2 1

线程2 2

线程2 3

线程2 4

线程2 5

线程2 6

线程2 7

线程2 8

线程2 9

线程2 10

线程2 11

线程2 12

线程2 13

线程2 14

线程2 15

线程2 16

线程2 17

线程2 18

线程2 19

线程2 20

线程1 21*/

 

事件

#include <iostream>

#include <windows.h>

int count = 0;

HANDLE hEvent;    //定义事件句柄

int s = ((hEvent = CreateEvent(NULL, FALSE, TRUE, L"event")), 1);

unsigned long __stdcall PrintCount1(void*)

{

         while (count < 20)

         {

                   WaitForSingleObject(hEvent, INFINITE);

                   count++;

                   std::cout << "线程1 " << count << "\n";

                   Sleep(100);

                   SetEvent(hEvent);

         }

         return 1;

}

unsigned long __stdcall PrintCount2(void*)

{

         while (count < 20)

         {

                   WaitForSingleObject(hEvent, INFINITE);

                   count++;

                   std::cout <<"线程2 "<< count << "\n";

                   Sleep(100);

                   SetEvent(hEvent);

         }

         return 2;

}

int main()

{

         CreateThread(NULL, 0, PrintCount1, NULL, 0, NULL);

         CreateThread(NULL, 0, PrintCount2, NULL, 0, NULL);

         Sleep(10 * 1000);

         return 0;

}

信号量

#include <iostream>

#include <windows.h>

//其他进程要用到本信号量的话不用CreateSemaphore,直接根据名字OpenSemaphore

//HANDLE OpenSemaphore(

//       DWORD fdwAccess,      //access

//       BOOL bInherithandle,  //如果允许子进程继承句柄,则设为TRUE

//       PCTSTR pszName  //指定要打开的对象的名字

 

int count = 0;

HANDLE hSemaphore= CreateSemaphore(NULL, 1, 100, L"sema"), 1); //最大资源100

unsigned long __stdcall PrintCount1(void*)

{

         while (count < 20)

         {

                   WaitForSingleObject(hSemaphore, INFINITE);

                   count++;

                   std::cout << "线程1 " << count << "\n";

                   Sleep(100);

                   long t;

ReleaseSemaphore(hSemaphore, 1, &t);//释放1个,释放前是t个。

         }

         return 1;

}

unsigned long __stdcall PrintCount2(void*)

{

         while (count < 20)

         {

                   WaitForSingleObject(hSemaphore, INFINITE);

                   count++;

                   std::cout <<"线程2 "<< count << "\n";

                   Sleep(100);

                   long t;

ReleaseSemaphore(hSemaphore, 1, &t);

         }

         return 2;

}

int main()

{

         CreateThread(NULL, 0, PrintCount1, NULL, 0, NULL);

         CreateThread(NULL, 0, PrintCount2, NULL, 0, NULL);

         Sleep(10 * 1000);

         return 0;

}

互斥体

#include <iostream>

#include <windows.h>

int count = 0;

HANDLE hMutex = CreateMutex(NULL, false, L"mutex");

unsigned long __stdcall PrintCount1(void*)

{

         while (count < 20)

         {

                   WaitForSingleObject(hMutex, INFINITE);

                   count++;

                   std::cout << "线程1 " << count << "\n";

                   Sleep(100);

                   ReleaseMutex(hMutex);

         }

         return 1;

}

unsigned long __stdcall PrintCount2(void*)

{

         while (count < 20)

         {

                   WaitForSingleObject(hMutex, INFINITE);

                   count++;

                   std::cout <<"线程2 "<< count << "\n";

                   Sleep(100);

                   ReleaseMutex(hMutex);

         }

         return 2;

}

int main()

{

         CreateThread(NULL, 0, PrintCount1, NULL, 0, NULL);

         CreateThread(NULL, 0, PrintCount2, NULL, 0, NULL);

         Sleep(10 * 1000);

         return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值