Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
1
Indexable
Never
void BlowOrDestroyThread()
{

	while (!blowDestroyMustExit.load(std::memory_order_relaxed))
	{
		printf("BlowOrDestroyThread\n");
		if (flag.load())
		{
			//std::this_thread::sleep_for(std::chrono::milliseconds(1500));

			int add = (test::Points::startPointCount-1) * p.nodeSize;
			for (int i = 0; i < p.nodeSize; ++i)// TODO:: if I put 64 here Counter thread hangs

			{

				int range = (test::Points::startPointCount) * p.nodeSize;

				int position = p.GenerateValidPosition(p.nodeSize - 1) + add;
				if (i % 2)
				{
					std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
					int time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
					printf("blow element position: %d, range: %d\n", position, range);
					p.SetInitDataOnPosition(position, time);



					p.AssignNewPoints(range);
					test::Points::startPointCount++;
				}
				
			    p.SetPointFlag(position, false);
			}
			flag.store(false);
		}
	}
}

// click on window initiate first points node and starts counter thread
void test::on_click(int x, int y)
{
	printf("on_click\n");
	std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();

	click++;
	if (!hitOnce)
	{
		//int time = globalTime.load(std::memory_order_relaxed);
		int time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
		p.SetInitData(x, y, time);

		p.AssignNewPoints(0);
		//printf(" 1! x: %d, y: %d range: %d\n", x, temp, range);
		Points::startPointCount++;
		counterThread = std::thread(PointsThread);
		blowDestroyThread = std::thread(BlowOrDestroyThread);
		blowDestroyThread.detach();
		flag.store(true);
		hitOnce = true;
	}
	else if(Points::startPointCount < p.nodesSize-1)
	{
		int range = Points::startPointCount * p.nodeSize;
		int temp = 765 - y;
		printf(" 1! x: %d, y: %d range: %d\n", x, temp, range);
		//int time = globalTime.load(std::memory_order_relaxed);
		int time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
		p.SetInitData(x, y, time);

		p.AssignNewPoints(range);
		Points::startPointCount++;

		//hitOnce = false;
		flag.store(true);
	}


}