Windows Thread (use event synchronization)

Source: Internet
Author: User

Windows threads (using event synchronization) are enhanced by 2008.10.9

  1. # Include <stdio. h>
  2. # Include <windows. h>
  3. // Thread Functions
  4. DWORD winapi mythreadproc (lpvoid lpparameter );
  5. // User-defined class
  6. Class mythreadparam {
  7. Public:
  8. Void myfunc ();
  9. };
  10. Handle EV; // event handle
  11. Int main (INT argc, char * argv []) {
  12. DWORD mythreadid; // thread ID
  13. Mythreadparam threadparam; // thread parameter, which can pass any type of pointer
  14. Handle h; // thread handle
  15. // Create an event. The initial event is "no signal" and is manually reset.
  16. Ev = createevent (null, true, false, "myeventname ");
  17. H = createthread (null, // thread attribute
  18. 0, // stack size, 0 use default stack size
  19. Mythreadproc, // thread function
  20. & Threadparam, // thread parameter, lpvoid type
  21. 0, // thread flag
  22. & Mythreadid // thread ID, output value
  23. );
  24. Printf ("thread ID is % d/N", mythreadid );
  25. // Wait for the event to signal, that is, wait for the thread to end
  26. Waitforsingleobject (EV, infinite );
  27. Printf ("thread % d terminated! /N ", mythreadid );
  28. // Reset the signal to no signal
  29. Resetevent (EV );
  30. Return 0;
  31. }
  32. DWORD winapi mythreadproc (lpvoid lpparameter ){
  33. Mythreadparam * Param = (mythreadparam *) lpparameter;
  34. For (INT I = 0; I <10; I ++ ){
  35. Param-> myfunc ();
  36. }
  37. // Set the event to a signal
  38. Setevent (EV );
  39. Return 0;
  40. }
  41. Void mythreadparam: myfunc (){
  42. Printf ("Hello thread! /N ");
  43. }

 

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.