C++ 并发指南-atomic原子变量使用struct

http://www.cplusplus.com/reference/atomic/atomic_store/

#include        // std::cout
#include          // std::atomic_flag
#include          // std::thread
#include          // std::vector
#include         // std::stringstream
#include 
#include 

struct MyStruct
{
	int li[1024];
	double msgl;
};

class a {
public:
	std::atomic<MyStruct > Mydata;
	// 线程标记
	std::thread battery_thread;
	void battery_thread_func();
};

void a::battery_thread_func()
{
	while (true) {
		for (int i = 0; i < 100; i++) {
			MyStruct ap;
			ap.li[0]=i;
			Mydata.store(ap, std::memory_order_relaxed);
			std::cout << "Mydata:" << Mydata.load(std::memory_order_relaxed).li[0]<<"\n";
			Sleep(100);
		}
	}
}

void main() {
	a li;
	li.battery_thread_func();
	while (true) {
		Sleep(1);
	}
}

你可能感兴趣的:(c++)