Multi-thread programming in Windows

Source: Internet
Author: User
Document directory
  • I. createthread () and waitforsignalobject () Usage
  • Ii. Thread mutex processing [reprint]
I. createthread () and waitforsignalobject () Usage
# Include "windows. H "# include" stdio. H "Void scan (char * Str) {printf (" % s ", STR); sleep (4000);} void main () {char * s =" NBA Yao year! \ N "; handle hthread = createthread (null, 0, (lpthread_start_routine) scan, S, 0, null); waitforsingleobject (hthread, infinite); Return ;}/ * parameter description: handle createthread (lpsecurity_attributes lpthreadattributes, // must be null, thread security DWORD dwstacksize, // generally 0, indicates that the stack is the same as the external size, thread depth lpthread_start_routine lpstartaddress, // The Name Of The thread function is lpvoid lpparameter, // The parameter passed to the thread function. If there are multiple parameters, the custom struct DWORD dwcreationflags. // 0 indicates that the thread is started immediately after the thread is created, if it is not started immediately, you need to call the resumethread function lpdword lpthreadid); // It is used to mark the thread name DWORD waitforsingleobject (handle hhandle, // handle to object to wait for DWORD dwmilliseconds // time-out interval in milliseconds); // wait until the hthread thread ends and infinite has no time limit */

 

Ii. Thread mutex processing [reprint]
# Include <windows. h> # include <iostream. h> DWORD winapi fun1proc (lpvoid lpparameter); // thread data DWORD winapi fun2proc (lpvoid lpparameter); // thread data int Index = 0; int tickets = 10; handle hmutex; void main () {handle hthread1; handle hthread2; // create thread hthread1 = createthread (null, 0, fun1proc, null, 0, null); hthread2 = createthread (null, 0, fun2proc, null, 0, null); closehandle (hthread1); closehandle (hthread2 );
// Create a mutex object
Hmutex = createmutex (null, true, "tickets"); If (hmutex) {If (error_already_exists = getlasterror () // error: already exists
{// If a mutex exists, release the handle and reset closehandle (m_hmutex); m_hmutex = NULL; cout <"only one instance can run! "<Endl; return ;}} waitforsingleobject (hmutex, infinite); releasemutex (hmutex); sleep (4000); // Let the main thread sleep for 4 seconds, let other threads have time to execute their code. If they do not sleep, the execution of other threads may fail or errors may occur. However, if the thread does not know how long it takes to execute the code, how long should it be written?} // DWORD winapi fun1proc (lpvoid lpparameter) // thread data {While (true) // infinite loop thread {waitforsingleobject (hmutex, infinite ); // obtain the mutex to run if (tickets> 0) {sleep (1); cout <"thread1 required Ticket:" <tickets -- <Endl;} else break; releasemutex (hmutex); // release mutex} return 0;} // DWORD winapi fun2proc (lpvoid lpparameter) // thread data {While (true) {waitforsingleobject (hmutex, infinite); If (T Ickets> 0) {sleep (1); cout <"thread2 restart Ticket:" <tickets -- <Endl;} else break; releasemutex (hmutex );} return 0;} // handle createmutex (lpsecurity_attributes lpmutexattributes, // the pointer to the Security Attribute bool binitialowner, // initialize the owner of the mutex (lpctstr lpname // pointer to the mutex object name); the parameter lpmutexattributes points to a security_attributes structure pointer, which determines whether the mutex handle is inherited by the quilt process. The binitialowner boolean type determines whether the creator of the mutex is the pointer of the owner lpname to the mutex name string. The mutex can have a name. The advantage of mutex is that it can be shared between processes.

 

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.