C ++ thread Test

Source: Internet
Author: User
// Threadtest. cpp: defines the console applicationProgram. // # Include "stdafx. H "# include <iostream> # include <windows. h> using namespace STD; handle hmutex; DWORD winapi fun (lpvoid lpparamter) {While (1) {waitforsingleobject (hmutex, infinite); cout <"Fun display! "<Endl; sleep (1000); releasemutex (hmutex) ;}} int _ tmain (INT argc, _ tchar * argv []) {handle hthread = createthread (null, 0, fun, null, 0, null); hmutex = createmutex (null, false, l "screen"); // This function is used to create an exclusive resource, the first parameter can be set to null, the second parameter specifies whether the resource belongs to the process that created it, and the third parameter specifies the Resource Name. // Create a resource closehandle (hthread) That is named screen and belongs to the process that creates it; while (1) {waitforsingleobject (hmutex, infinite ); // The first parameter specifies the handle of the requested resource, and the second parameter is generally set to infinite, indicating that if the requested resource is not applied for, it will wait until the resource is reached. // if it is set to 0, it indicates that the system will return if no resource is available. You can also specify how long it will wait to return, in the unit of 1‰ seconds. Cout <"main display! "<Endl; sleep (1000); releasemutex (hmutex); // This function is used to release an exclusive resource. Once a process releases this resource, it no longer belongs to it, if you want to use it again, you need to apply for the resource again .} return 0 ;}

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.