I want to know what I have to do until the next loop if I finish with a multi-threaded game loop.

Asked 1 years ago, Updated 1 years ago, 330 views

Question details

I'd like to know how to make a game loop process.As it is asynchronous, one of them ends first, so what should I do then?

What I checked and found

An object to be drawn by calculation is calculated and drawn in the next frame.In the meantime, the calculation thread calculates the position of the next frame and draws it based on the next frame.

What's happening

While drawing, another thread performs the calculation, so
For example, if the calculation is completed first, the drawing will draw based on the calculated value, so extreme talk will occur, and the calculation will be drawn in that position with reference to two values during one drawing.

What do you want to know

1. I want to know what to do until the drawing is finished, for example, when the calculation is finished earlier than the drawing in the processing in which the drawing and the calculation are respectively operated by an independent threads.What should I do in the opposite case?提示Please refer to the presented image

2. If you want to create a program such as movement, you should multiply the delta time. Is this delta time for drawing or delta time for calculation?

About Presentation Codes

Calculation and drawing loops
Implemented in std::thread.

environment, utilization library

utilization libraries:opengl,glew,glfw,glm
OS:ubuntu
Language: C++

Presentation image

Enter a description of the image here

Reference Site

Blog site: https://sites.google.com/site/monshonosuana/opengl%E3%81%AE%E8%A9%B1/opengl%E3%81%AE%E8%A9%B1-%E7%AC%AC5%E5%9B%9E

Source Code

#include<iostream>
# include <thread>
# include <future>
# include <memory>
# include <ctime>
# include <unistd.h>
# include <random>


std::random_device rnd;// Generate non-deterministic random number generator
std::mt19937mt(rnd()); // 32-bit version of Mersenne Twister, argument is initial seed value
    
void thread_Update()
{
    std::uniform_int_distribution<>land(0,10);//[0,99] uniform random number in the range

    for (inti=0; i<10;i++)
    {
        std::cout<<"thread_A"<<std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(land(mt)*100));

    }
    
}


void thread_Render()
{
    std::uniform_int_distribution<>land(0,10);//[0,99] uniform random number in the range

    for (inti=0; i<10;i++)
    {
        std::cout<<"thread_B"<<std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(land(mt)*100));
    }

}

int main()
{
    try
    {
        std::thread t1(thread_Render);
        std::threadt2(thread_Update);

        std::cout<<"end"<<std::endl;

        std::this_thread::sleep_for (std::chrono::milliseconds(5000));
        t1.join();
        t2.join();

    }
    catch(std::exception&e)
    {
        std::cout<<e.what()<<std::endl;
    }


    return 0;
}

c++ opengl

2022-12-30 06:15

1 Answers

Question 1 can be asked according to the specifications.

  • If you don't mind abandoning your partner's results, proceed with your own actions without waiting for them to do so
  • If you must not abandon the other party's results,
    • Wait for the other party to take action.or
    • Queue the processing results to proceed with your own processing.When the queue is full,
      • Wait until the queue is empty.or
      • Expand the queue size.or
      • Throw out old results in the queue.一部Some of them will be thrown away, but
  • Wait for the other party to take action.or
  • Queue the processing results to proceed with your own processing.When the queue is full,
    • Wait until the queue is empty.or
    • Expand the queue size.or
    • Throw out old results in the queue.一部Some of them will be thrown away, but
  • Wait until the queue is empty.or
  • Expand the queue size.or
  • Throw out old results in the queue.一部Some of them will be thrown away, but

Question 2 has nothing to do with Question 1, so please ask a different question.


2022-12-30 17:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.