Giter VIP home page Giter VIP logo

leftright's People

Contributors

mmcshane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

andrewjd79

leftright's Issues

Multiple function definition, link error.

Definition of functions atomic_reader_registry::depart, atomic_reader_registry::arrive, atomic_reader_registry::empty misses inline keyword.
Non template class function declared in header must be inline.

Interesting behaviour

Hi i have decided to test leftright implementation of yours.

write thread updates leftright protected Big struct.
read thread reads updated values and checks if read struct is half updated.

I have run this test on x86_64 cpu and i read half updated values.

I dont think this is the expected behaviour but want to ask anyways.
Is this expected behaviour?

Sample output
failed 23096 23097 23097 23097
failed 72977 72977 72977 72979
failed 109959 109959 109959 109960

Here is the test code.

struct Big
{
private:
	alignas(std::hardware_destructive_interference_size) int a1{};
	alignas(std::hardware_destructive_interference_size) int a2{};
	alignas(std::hardware_destructive_interference_size) int a3{};
	alignas(std::hardware_destructive_interference_size) int a4{};

public:
	void add1()
	{
		a1 += 1;
		a2 += 1;
		a3 += 1;
		a4 += 1;
	}

	void print() const
	{
		std::cout << a1 << ' ' << a2 << ' ' << a3 << ' ' << a4 << std::endl;
	}

	void check() const
	{
		if ((a1 == a2) && (a1 == a3) && (a1 == a4))
		{
		}
		else
		{
			std::cout << "failed ";
			this->print();
		}
	}
};

template <typename T>
struct leftright_wrap
{
	mpm::leftright<T> lr;

	auto read() const noexcept
	{
		return lr.observe([](const auto &val) noexcept
						  { return val; });
	}

	auto write(const T &t) noexcept
	{
		lr.modify([&t](auto &val) noexcept
				  { val = t; });
	}
};

int main()
{

	leftright_wrap<Big> lr;

	std::thread read_([&lr]
					  {
						  while (true)
						  {
							  auto a = lr.read();
							  a.check(); //! should not see half updated value
						  }
					  }

	);

	std::thread write_([&lr]
					   {
						   Big big{};
						   while (true)
						   {
							   big.add1();
							   lr.write(big);
						   }
					   }

	);

	read_.join();
	write_.join();
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.