multi-thread programming in c++

Discussion in 'C++' started by kolonel, Jul 15, 2009.

  1. kolonel

    kolonel New Member

    Joined:
    Jul 15, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    I'm new in c++ thread programming, so I made a short program to understand how threads work in c++. However, the result is not what I expected, could anybody please take a look at this code and see if I did something wrong:

    There is a glob_temp_var whose value is updated at each thread calling and it is printed out at two points: point 1 and point 2, points before and after updating in thread function. Although the glob_temp_var is changing inside the thread function, in point 2 the not-updated value of that, i.e. the value before going into the thread function, is printed out!
    I think it is not correct, does anybody have any explanation or suggestion?
    Thanks.

    Code:
    #include <iostream>
    #include <windows.h>
    #include <process.h>
    #include <time.h>
    
    HANDLE hEvent[MAX_CPU_COUNT];
    HANDLE StartEvent[MAX_CPU_COUNT];
    
    int glob_temp_var=0;
    
    static void Thread1 (LPVOID lpParam){
        while (true){
            WaitForSingleObject(StartEvent[0], INFINITE);
            glob_temp_var++;
            ResetEvent(StartEvent[0]);
            SetEvent(hEvent[0]);
        }
    }
    
    int
    main(int argc, char** argv)
    {
        for (int i=0; i<MAX_CPU_COUNT; i++){
            hEvent[i] = CreateEvent( NULL, TRUE, TRUE, NULL );
            StartEvent[i] = CreateEvent( NULL, TRUE, TRUE, NULL );
        }
    
        _beginthread( Thread1, 0, NULL );
    
        Sleep(1000);
    
        for (int t=0;t<duration;t++){      
            std::cout<<glob_temp_var;      //////////////// point 1
    
            for (int j=0; j<MAX_CPU_COUNT; j++)        
                SetEvent(StartEvent[j]);
            
            WaitForSingleObject(hEvent[0], INFINITE );
            
            for (int j=0; j<MAX_GPU_COUNT; j++)    
                ResetEvent(hEvent[j]);
    
            std::cout<<glob_temp_var;      //////////////// point 2
        }
    
        for (int j=0; j<MAX_CPU_COUNT; j++){
            ResetEvent(StartEvent[j]);
            ResetEvent(hEvent[j]);
        }
     CUT_EXIT(argc, argv);
    }
     
    Last edited by a moderator: Jul 16, 2009
  2. hotnspicy

    hotnspicy New Member

    Joined:
    Jul 6, 2009
    Messages:
    44
    Likes Received:
    4
    Trophy Points:
    0
    clera glob_temp_var before calling it each time
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Just looking at the code doesn't give any clue about your intent; could you describe step by step what you think the program does and why?

    Do you have optimisations switched off? It's possible the compiler might think glob_temp_var hasn't changed and therefore print out the same value it printed before, rather than generating code to go back to memory and actually take a look. You may need to define glob_temp_var volatile.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice