CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2005
    Location
    VA USA
    Posts
    7

    a question about multiplethread

    now i use several threads to wite a serial port ,i want only one thread to write the port at the same time ,I don't know how to do it . i hope some one help me soon

  2. #2
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: a question about multiplethread

    Take a look at Mutex's and Critical Sections.
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

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

    Re: a question about multiplethread

    [ redirected ]

    Regards,
    Siddhartha

  4. #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