Skip to content

Commit

Permalink
Updated idea.md; Added timed_mutex to updateDisplay
Browse files Browse the repository at this point in the history
  • Loading branch information
adi-g15 committed Nov 25, 2020
1 parent 51244f8 commit dbb7d67
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
11 changes: 8 additions & 3 deletions Idea.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
A parallel world simulator... similar to snakes game, just that there are 4 snakes. Now each chases the food, and as such, each others presence matters, and as one gets food, its value incremented by one, so each 'action'(only one action in this world, that is to grab food), Two things the program does -> Remove a snake, this creates another graph(ie. a world), and simulates it from the origin point,(ie. each has 0 points), without presence of the removed one, and we get the situation at current (which will definitely be different, since the chances of the action being done by one is 1/3rd instead of 1/4th. The second action is... chose a snake, chose a time point (like 3 food earlier), and then chose a position, at which the snake should have been at that time... this action creates another graph(ie. a world), `from that point of time, it will rerender through actions and give the situation at current point`, this world 'can' be different (degree of difference, the divergence number varies, according to chosen position is likely to give it the most food in future moves (future to that chosen past, and is actually the present)).... this is how it works. It may seem simple, but indeed GREAT :D !!

* Can all snakes be put on different threads, this will greatly make it more real world, not waiting for a move... though one important thing will be that two snakes don't try to eat the same food, at same point of time
> But, i now think, this can highly affect our ability to log time points as simple integers ?
* [Theoretically Implemented] Can all snakes be put on different threads, this will greatly make it more real world, not waiting for a move... though one important thing will be that two snakes don't try to eat the same food, at same point of time
> [Unsolved] But, i now think, this can highly affect our ability to log time points as simple integers ?
* There can be a rate at which the world will keep growing, and at the moment, the world doesn't have enough space, enough storage will be made, as well as increases the spread rate... Or it can be made such that the rate itself increase too with course of time
* [Theoretically Implemented] There can be a rate at which the world will keep growing, and at the moment, the world doesn't have enough space, enough storage will be made, as well as increases the spread rate... Or it can be made such that the rate itself increase too with course of time

Domain of e -> when length reaches a particular 10e4 or similar

Allocator -> All worldPlots consult this global class to allocate data, it basically keeps count of how much data allocated
For now, it can simply just have about 1 function which just returns pointer to location of object type... we don't really need to decrease the counter or something after dweletion, since there is just so less deleteion happeninng in our case
2 changes: 1 addition & 1 deletion display/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Display : protected single_term, public std::enable_shared_from_this<Displ
void printScreen();
void updateScreen();

std::condition_variable event_convar;
// std::condition_variable event_convar; // not needed, work done by mutex
std::mutex event_mutex; // used to lock mutex so that ONLY ONE thread accesses it during each time interval the display is active
void startEventLoop();

Expand Down
7 changes: 7 additions & 0 deletions display/src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ void Display::printScreen(){
}

void Display::updateScreen(){
using namespace std::chrono_literals;
if ( ! this->dispMutex.try_lock_for(1s)) { // there will be gap of ATLEAST 1 second before any updations happen on screen
return; // so repeating threads in same interval of time, will exit
}

this->dispMutex.lock();
if( !top_area || !main_area || !legend_area )
this->runInitTasks();

Expand Down Expand Up @@ -208,6 +213,7 @@ void Display::updateScreen(){
top_area->addstr(title, position::MIDDLE);

wrefresh(stdscr);
this->dispMutex.unlock();

}

Expand All @@ -228,6 +234,7 @@ void Display::runInitTasks(){

}

// @deprecated
void Display::render(){
using namespace std::chrono_literals;

Expand Down
7 changes: 0 additions & 7 deletions src/snake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,13 @@ void Snake::simulateExistence(){

// a one time check, before an entity starts existing
if( std::this_thread::get_id() == this->parent_world->_shared_concurrent_data.get_world_thread_id() ){
#ifdef NO_THREAD_ENTITY
continue;
#endif
throw std::logic_error( "Entities should be on a different thread, than the world. In case, you don't want this behaviour, then pass -DNO_THREAD_ENTITY (To define NO_THREAD_ENTITY)" );
}

while ( this->parent_world->_shared_concurrent_data.is_world_running() ){ //while the parent world continues to exist keep the entity moving
this->moveForward();

#ifdef NO_THREAD_ENTITY
continue; // don't pause the current thread, if it's the main thread itself
#endif
std::this_thread::sleep_for( std::chrono::milliseconds( (int)statics::UNIT_TIME * 1000 ) );

}
}

Expand Down

0 comments on commit dbb7d67

Please sign in to comment.