Archived

This topic is now archived and is closed to further replies.

Multithreads & Pointer to bool :: MFC

This topic is 5649 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Ought to be volatile bool*, if you want to ensure it works as desired in a release build.

volatile tells the compile that it can''t use any short-cuts to evaluate the status of the bool - that it must check that memory every time. This is essential for atomic syncronization of multi-threaded code such as yours. MSVC is pretty good about detecting these situations though, and will often treat it as volatile even if you don''t specify it as such.

Share this post


Link to post
Share on other sites
Okay. Thanks.

A member at cprogramming 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

Share this post


Link to post
Share on other sites
What is ptp? Is the memory pointed to by ptp->pBVariable allocated?

ptp->pBVariable will probably need to be declared volatile as well.

You might want to look into events and wait functions.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
If one thread accesses temp by value and the other through pointer, both should be declared volatile.

Note that a volatile is a modifier like const: you can''t convert implicitly between say bool * and volatile bool *: you need a cast for that.

Share this post


Link to post
Share on other sites