Thread: Multithreads & Pointer to bool :: MFC

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348

    Multithreads & Pointer to bool :: MFC

    Hi.

    I have an weird problem with multithreading. I implemented a simple worker thread. I pass to it two pointers to two variables. One is an integer, and the second is a bool. Both are pointers to private data members in doc. Now, I use the bool as a way to "stop" the worker thread at any given moment. For example, if the use wanted to stop the process for any reasons, the user can click the "stop" button, which would then set the bool variable passed that was passed to the worker thread to false.

    Inside the worker thread, I call a function and pass it a pointer to the bool variable. The program compiles without a problem; however, it crashes at the point where it checks the status of the pointer to the bool variable. I have tried different technique including passing in a pointer to a pointer, etc. None has worked. Here is a summary.

    -----
    WorkerThreadFunc(...)
    {
    bool *pBoolVariable = ptp->pBVariable;

    // X is a pointer to a class object
    classX *cX = ptp->X;

    // passing a pointer to a bool variable
    cX->myFunction(pBoolVariable,...)
    -----

    The problem will crash when it checks the status of pBoolVariable inside myFunction.

    -----
    if (*pBoolVariable)
    ...
    -----

    I am not sure if I need to change the way I am passing the bool variable to the function. I have tried passing in a reference, but the program crashs the same way. The algorithm above works only if pBoolVariable is not passed into myFunction. Weird.

    Please post if you see a possible problem.

    Thanks,
    Kuphryn

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. The problem was has to do with human error. I have two functions with the same parameters and very similar names. In one of them, I forgot to pass in the data members variable for pBoolVariable. The solution was to add one line of code to pass it into the worker thread.

    Kuphryn

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Don't forget to mark the variables you use in both threads as volatile, or your compiler might optimize it when you don't want it optimized.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Okay. Thanks.

    A member at GameDev also brought up the point o marking the bool as "volatile." Can you explain the term relative to C++ and multithreading, when to use it, and how to set the compiler to "mark" it volatile?

    Thanks,
    Kuphryn

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Its a keyword that does nothing noticable, but stops the compiler making a certain optimisation that can make multiple thread access to a single variable rather problematic....

    As I understand it, when compilers see that a certain variable's value will not be altered by code in a thread, but that thread's execution is reliant on that value, the compiler will try to move the value of that memory location into one of the processor's registers....this allows the code to execute very quickly as its quicker to address a register than a location in memory......

    The problem is that it might not take into account that another thread may affect the value....and as that variable is not being accessed by the first thread (only a copy of its value being held in a register) then the first thread is operating oblivious to the fact that the variable has changed.....so if you mark the variable "volatile", it urges the compiler not to cache the value into a register....

    Mark it like so;

    Code:
    volatile BOOL bIsItTrue;

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Interesting. Thanks.

    I do not remember Barjne Stroustrup mentioning volatile in C++ reference.

    Okay. What variable should you declare volatile? For example:

    -----
    bool temp;
    temp = true;

    // passing a pointer of temp into a worker thread

    WORKERPARM ptp = new WORKERPARAM;
    ptp->pTemp = &temp;
    -----

    In the example above, do you declare "temp" volatile, "pTemp" volative, or both?

    Kuphryn

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by kuphryn
    Interesting. Thanks.

    I do not remember Barjne Stroustrup mentioning volatile in C++ reference.
    He is rather vague on the subject...he states that volatile is a "hint" to the compiler that the variable may change in a way "not specified by the language"......remember that threads are not really under consideration for the standard......each platform will implement them differently and tying that ability into a 1 off unifying standard would probably be unworkable and rather unrealistic.....therefore threads are not mentioned much......I guess its kind of weird that volatile got included at all!
    Last edited by Fordy; 06-25-2002 at 03:16 PM.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Yeah. I do not remember volatile modifier at all until members at this forum and GameDev brought it up.

    Kuphryn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  2. BOOL bool ? unresolved external symbol
    By xwielder in forum C Programming
    Replies: 6
    Last Post: 05-20-2008, 08:39 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. How do I play an MP3?
    By Hunter2 in forum Windows Programming
    Replies: 28
    Last Post: 05-20-2002, 08:49 PM