Problem checking bool.

Hello, I have a class with 2 bools and inside the class I have a function that toggles the bool when the user presses a key. Like this:

1
2
3
4
5
6
7
  class Settings
{
public:
	bool Toggle1 = true;
	bool Toggle2 = true;
	void Loop(); // this is a thread that is created from main, it checks if user presses a key and then flips the bool like this: Toggle1 = !Toggle1
};


I know the bool changes because I print it out to in console to test from the Loop thread, but when I try to check bool value in another class, it always reads the initial value not the changed value. This is how I check it:

1
2
3
4
while (S.Toggle1 == true) //S is Settings class object
{
//execute code
}


I know this is a newbie mistake and probably a very simple thing, but I really cannot figure this out.
Last edited on
Not enough code to tell what could be wrong.

Are you sure you're not checking against a default constructed copy of your Setting instance?
Is S global? Or is S a member variable of the other class?

Last edited on
This is my Loop() function which checks if user presses a key and then flips the bool.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void Settings::Loop()
{
	PrintToggles();

	while (true)
	{
		if (GetAsyncKeyState(VK_F6))
		{
			Toggle1 = !Toggle1;
			PrintToggles();
			ToggleSound();
		}

		if (GetAsyncKeyState(VK_F7))
		{
			Toggle2 = !Toggle2;
			PrintToggles();
			ToggleSound();
		}

		Sleep(100);
	}
}


My other function in a different class which checks if bool is true then execute code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void AnotherClass::Loop()
{
	while (true)
	{
		while (S.Toggle1 == true)
		{
			// Do stuff

			Sleep(1);
		}

		Sleep(1);
	}
}


I made "S" object using "extern Settings S;" in "Settings.h" and adding "Settings S;" in my main source file.
> when I try to check bool value in another class, it always reads the initial value not the changed value

1
2
3
4
5
6
7
8
9
#include <atomic>

class Settings
{
public:
	std::atomic<bool> Toggle1 { true };
	std::atomic<bool> Toggle2 { true };
	void Loop(); 
};

Each instantiation and full specialization of the std::atomic template defines an atomic type. If one thread writes to an atomic object while another thread reads from it, the behavior is well-defined (see memory model for details on data races)
http://en.cppreference.com/w/cpp/atomic/atomic
Tried with std::atomic, got this error.
1
2
Severity	Code	Description	Project	File	Line	Suppression State
Error	C2664	'std::tuple<void (__thiscall Settings::* )(void),Settings>::tuple(std::tuple<void (__thiscall Settings::* )(void),Settings> &&)': cannot convert argument 1 from 'void (__thiscall Settings::* )(void)' to 'std::allocator_arg_t'	Project	c:\program files (x86)\microsoft visual studio 14.0\vc\include\memory	1630	
std::atomic<bool> (and consequently Settings) is neither copyable nor movable.

To pass an object of type Settings to a thread's constructor, wrap it with std::ref().
http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
http://en.cppreference.com/w/cpp/utility/functional/ref

Alternatively, pass the address of the object (a pointer) to the thread's constructor.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <atomic>
#include <thread>
#include <functional>
#include <chrono>
#include <iostream>

class Settings
{
    public:
    std::atomic<bool> Toggle1 { true };
    std::atomic<bool> Toggle2 { true };
    void Loop();
};

using namespace std::literals ;

void thread_fun( Settings& s ) // pass reference
{
    for( int i = 0 ; i < 10 ; ++i )
    {
        std::this_thread::sleep_for( 200ms ) ;
        s.Toggle1 = !s.Toggle1 ;
    }
}

int main()
{
    Settings s ;
    std::thread t( thread_fun, std::ref(s) ) ; // pass wrapped reference

    for( int i = 0 ; i < 20 ; ++i )
    {
        std::this_thread::sleep_for( 100ms ) ;
        std::cout << std::boolalpha << s.Toggle1 << '\n' << std::flush ;
    }

    t.join() ;
}

http://coliru.stacked-crooked.com/a/89e46406ba8846e6
Topic archived. No new replies allowed.