CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: a question about multiplethread

    The easiest approach to restricting the number of threads that execute a piece of code to one is via Critical Sections.

    One works with Critical Sections using Win APIs -

    So, in your case, the code would be something like this -

    Creating and Initializing:
    Code:
    // A global object, or a globally accessible object (like static member)
    CRITICAL_SECTION myCS;
    
    // In the main thread
    ::InitializeCriticalSection (&myCS);
    Using Critical Sections:
    Code:
    // Inside the worker thread
    
    // At the point in code that needs to be protected
    // And needs to be entered one thread at a time
    
    ::EnterCriticalSection (&myCS);
    
    // The lines of code that need to be protected
    
    ::LeaveCriticalSection (&myCS);
    Finally, Deleting:
    Code:
    // Again in the main thread, after the worker threads
    // Have ended and the CS is not needed
    ::DeleteCriticalSection (&myCS);
    Hope, you now have a clear idea of the way to proceed.
    Last edited by Siddhartha; October 15th, 2005 at 05:43 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured