dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: [RESOLVED] how make a multithread synchronization correctly?

  1. #16
    Join Date
    Nov 2003
    Posts
    1,902

    Re: [RESOLVED] how make a multithread synchronization correctly?

    No test is needed. When two or more threads access the same memory location, and at least one of those threads is writing then proper synchronization is needed - otherwise you have unsynchronized access / data races. This is the case on the mentioned lines.

    gg

  2. #17
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,172

    Re: [RESOLVED] how make a multithread synchronization correctly?

    Quote Originally Posted by Cambalinho View Post
    sorry... on your test do you use SingleThreadAtTime(true)? yes these make sure that waits until the last thread is finished
    With regard to your comment of
    //delay for avoiding execution of all threads in same time and for not losing data because of that if(blnCreationOrder==true) delay(100);
    As codeplug mentioned, this isn't the proper way to synchronize access to a shared resource. You need to use a synchronization primitive (e.g. critical section or mutex) to do so.

    Spreading threads apart with a delay does nothing to help synchronize resource access.

  3. #18
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,920

    Re: [RESOLVED] how make a multithread synchronization correctly?

    Before you go much further, I would suggest you read and digest this book 'C++ Concurrency In Action'
    http://www.amazon.co.uk/C-Concurrenc...multithreading

    You will then have an understanding of the concurrency issues within the code that Arjay, Codeplug, myself et al have highlighted. Multi-threaded code needs to be built from strong foundations. Just doing a 'few tests' that seem 'to work' does not mean that the code is correct! As an example, having delays in multi-threaded code to 'make it work' is always a sign of incorrect design/implementation. Correct multi-threaded code doesn't have/need these 'delays'.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  4. #19
    Join Date
    Apr 2009
    Posts
    1,243

    Re: [RESOLVED] how make a multithread synchronization correctly?

    i found the book.. thanks.
    i will share it, if i have permition(the link where i download it)
    to be honest that class is working great and i had tested. i'm using it on read() and write(). it's great

  5. #20
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,172

    Re: [RESOLVED] how make a multithread synchronization correctly?

    Quote Originally Posted by Cambalinho View Post
    to be honest that class is working great and i had tested. i'm using it on read() and write(). it's great
    Actually, you are getting lucky and it only appears to work on the environment you are testing it on.

    With multithreading code luck only gets you so far, so look forward to some debugging sessions when the code you thought 'works great' starts failing intermittently on other machines.

  6. #21

    Re: [RESOLVED] how make a multithread synchronization correctly?

    I have read and digest this book 'C++ Concurrency In Action'. Very intresting and ushelp.

Page 2 of 2 FirstFirst 12

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)