Windows system calls semaphore semaphore

Source: Internet
Author: User
Tags semaphore

1#include"iostream"2#include"windows.h"3#include"CString"4 using namespacestd;5 6HANDLE g_hsemthreads=Invalid_handle_value;7 8 StaticDWORD WINAPI ThreadProc (lpvoid lpparam) {9LONG npausems=reinterpret_cast<long>(Lpparam);Ten Sleep (npausems); One  A     if(g_hsemthreads!=Invalid_handle_value) { -LONG NPREVCT (0); -  the         if(ReleaseSemaphore ( - G_hsemthreads, -             1, -&NPREVCT +             )){ -printf"%d msec thread ended, slots left:%d\n", npausems,nprevct+1); +         } A     } at  -     return(0); - } -  -  -  in voidMain () { -g_hsemthreads=CreateSemaphore ( to NULL, +         5, -         5, the NULL *         ); $      for(intNtotal= -;ntotal>0; ntotal-=2){Panax Notoginseng WaitForSingleObject (g_hsemthreads,infinite); -LONG npausems=ntotal*5; the  +HANDLE hthread=CreateThread ( A NULL, the             0, + ThreadProc, -Reinterpret_cast<lpvoid>(Npausems), $             0, $ NULL -             ); -  theprintf"In the %d msec a thread is creates \ n", npausems); - CloseHandle (hthread);WuyiHthread=Invalid_handle_value; the     } -  Wu CloseHandle (g_hsemthreads); -g_hsemthreads=Invalid_handle_value; About GetChar (); $}

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.