Thread: Multithreading not working as expected

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    120

    Multithreading not working as expected

    Code:
    #include <iostream>
    #include <windows.h>
    #include <process.h>
    
    int x = 0, y = 0;
    
    void myFunc( void* arg ) {
    
        for(;;) {
            ++x;
            ++y;
        }
    
    }
    
    int main() {
    
        _beginthread( myFunc, 0, 0 );
    
        for(;;) {
            std::cout <<x <<" " <<y <<"\n";
        }
    
    }
    My problem is that x and y are always 0, but if i had a sleep() to the threads entry function:
    Code:
    void myFunc( void* arg ) {
    
        for(;;) {
            ++x;
            ++y;
            Sleep(100); //Or even Sleep( 1 )
        }
    
    }
    x and y change correctly. Why is that?

    Just to make it clear, ive just started with multithreaded programming.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    When two or more threads access the same memory location (variable), and at least one thread is writing, then you must use synchronization primitives provided by your OS or threading library.

    Under Windows, a CRITICAL_SECTION is a common synchronization object for accessing shared resources.

    gg

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    120
    my question ATM is not "how can i..." but rather, "Why did it work with the sleep function??"

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It didn't "work". You did it wrong two different ways and observed different results. If you want to explore undefined behavior, then you need to look at the assembly code emitted by your compiler, and understand what it means on your particular hardware.

    In this case however, I can guess the "why". The compiler optimized accesses to x and y by placing them into CPU registers. When you introduced the call to Sleep, the compiler could not guarantee that the innards of Sleep don't modify x and y, since they have external linkage. Because of this, the compiler could not place x and y into registers.

    gg

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    120
    good answer thank you that put my mind at easy.

    I didnt realy tought of that, Im not used to take the compilers optimizations and changes in conssideration, I know i should, and maybe from now on i will pay more attention to that detail.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also consider that main may exit before your thread even starts.
    Since you're using C++, I'd recommend boost's thread library, which includes threading synchronization, as well as C++ threads.

    But there is an easy way to detect these problems with some simple design.
    Whenever you have to threads accessing the same variable, you MUST make a decision. IF at least one thread writes to the variables, then you MUST use synchronization primitives. As simple as that. Note that if both threads reads from the variable (but none writes), then you can get away with not using any synchronization.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program not working as expected
    By perrrson in forum C Programming
    Replies: 3
    Last Post: 10-02-2010, 01:49 PM
  2. Pipes not working as expected
    By Shadow_Fiend in forum C Programming
    Replies: 1
    Last Post: 05-16-2010, 05:55 AM
  3. srand() no working as expected
    By guesst in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2009, 12:24 PM
  4. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM