PNS Windows_37_thread_interlock Thread-Atomic lock

Source: Internet
Author: User

Windows_37_thread_interlock Thread-Atomic lock
  
 
  1. // windows_37_Thread_InterLock.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "windows.h"
  5. long g_nValue1 = 0;
  6. long g_nValue2 = 0;
  7. DWORD WINAPI InterProc1( LPVOID pParam )
  8. {
  9. for (int nIndex = 0; nIndex < 10000000;nIndex++)
  10. {
  11. //普通的加加
  12. g_nValue1++;
  13. }
  14. return 0;
  15. }
  16. DWORD WINAPI InterProc2( LPVOID pParam )
  17. {
  18. for (int nIndex = 0; nIndex < 100;nIndex++)
  19. {
  20. //原子锁加加
  21. //g_nValue2++;
  22. InterlockedIncrement( &g_nValue2 );
  23. }
  24. return 0;
  25. }
  26. void Create( )
  27. {
  28. DWORD nThreadID = 0;
  29. HANDLE Hthread [ 4 ] = { NULL
  30. HThread [ 0 ] Span class= "pun" >= createthread ( NULL 0 interproc1 , NULL 0 &
  31. HThread [ 1 ] Span class= "pun" >= createthread ( NULL 0 interproc1 , NULL 0 &
  32. HThread [ 2 ] Span class= "pun" >= createthread ( NULL 0 interproc2 , NULL 0 &
  33. HThread [ 3 ] Span class= "pun" >= createthread ( NULL 0 interproc2 , NULL 0 &
  34. WaitForMultipleObjects( 4, hThread, FALSE, INFINITE );
  35. printf( "%ld\n", g_nValue1 );
  36. printf( "%ld\n", g_nValue2 );
  37. }
  38. int _tmain(int argc, _TCHAR* argv[])
  39. {
  40. Create( );
  41. return 0;
  42. }



From for notes (Wiz)

PNS Windows_37_thread_interlock Thread-Atomic lock

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.