Stuff after the for loops performing before it's done

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

41 Replies - 1596 Views - Last Post: 28 January 2019 - 06:37 PM Rate Topic: -----

#1 tolazytbh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 24-December 18

Stuff after the for loops performing before it's done

Posted 19 January 2019 - 08:38 PM

#include "Engine.h"

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <iostream>
#include <thread>

Engine::Engine()
{
    active = true;
    cThread = std::thread(&run, this);
    curScreen.resize(1297000);
    std::cout << curScreen.size() << std::endl;
    std::cout << "Initialized" << std::endl;
}


void Engine::run() {
    std::unique_lock<std::mutex> lock(mtx);
    while (active) {
        std::cout << "Active" << std::endl;
        //if(animation) { } else {
        perform.wait(lock);
        //int i = 0;
        //int b = 0;
        int g = 0;
        //try {
        std::cout << "Start " + sizeof(curScreenTile) << std::endl;
        for(int i = 0; i < 1430; i++) {
            for(int b = 0; b < 890; b++) {
                std::cout << "Start2" << std::endl;
                //std::cout << "at: " + g << std::endl;
                curScreen.at(g).setActive(true, curScreen.at(g));
                curScreen.at(g).setColor(0,240,0, curScreen.at(g));
                curScreen.at(g).setPos((short) i, (short) b, 1, curScreen.at(g));
                g++;
            }
        }
        std::cout << "End" << std::endl;
        //} catch(...) {
           // std::cout << "failed" << std::endl;
        //}
        update = true;
        //}
        std::cout << "Tasks Performed" << std::endl;
    }
}

int Engine::getSize() {
    return curScreen.size();
}

bool Engine::getIsActive(int position3) {
    return curScreen.at(position3).active(curScreen.at(position3));
}

RECT Engine::getRectOf(int position) {
    return curScreen.at(position).getR(curScreen.at(position));
}

unsigned long Engine::getColorOf(int position2) {
    return curScreen.at(position2).getColor(curScreen.at(position2));
}

bool Engine::processing() {
    return inProcess;
}

void Engine::setProcess(bool var) {
    inProcess = var;
}

bool Engine::getUpdate() {
    return update;
}

void Engine::done() {
    update = false;
    inProcess = false;
}

void Engine::notify() {
    perform.notify_one();
}


Engine::~Engine()
{
    active = false;
    //dtor
}




#include <atomic>
#include <condition_variable>
#include <mutex>
#include <iostream>
#include <thread>
#include <vector>
#include <windows.h>

#ifndef ENGINE_H
#define ENGINE_H


struct curScreenTile {
        RECT r;
        unsigned long rgb;
        bool activeTile = false;

        static void setColor(int b, int g, int r, curScreenTile tile) {
            tile.rgb = ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
        }

        static void setPos(short x, short y, short pixelSize, curScreenTile tile) {
                tile.r.left = x;
                tile.r.top = y;
                tile.r.right = y+pixelSize;
                tile.r.bottom = x+pixelSize;
        }

        static void setActive(bool toSet, curScreenTile tile) {
            tile.activeTile = toSet;
        }

        static RECT getR(curScreenTile tile) {
            return tile.r;
        }

        static unsigned long getColor(curScreenTile tile) {
            return tile.rgb;
        }

        static bool active(curScreenTile tile) {
            return tile.activeTile;
        }
};



class Engine
{
    public:
        Engine();
        void run();
        void notify();
        int getSize();
        bool getIsActive(int position3);
        RECT getRectOf(int position);
        unsigned long getColorOf(int position2);
        bool getUpdate();
        bool processing();
        void setProcess(bool var);
        void done();
        virtual ~Engine();

    protected:

    private:
        std::mutex(mtx);
        std::condition_variable perform;
        std::thread cThread;
        std::vector<curScreenTile> curScreen;
        bool active = false;
        bool update = false;
        bool inProcess = false;
};

#endif // ENGINE_H




Everything seems to be working except the std::out << "End" and update = true is happening before it is done

Also is that the correct way to find out the data size of the Struct, It's not recreating the static variables like it doesn't in java?

Is This A Good Question/Topic? 0
  • +

Replies To: Stuff after the for loops performing before it's done

#2 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2295
  • View blog
  • Posts: 4,397
  • Joined: 30-May 10

Re: Stuff after the for loops performing before it's done

Posted 19 January 2019 - 10:16 PM

How do you know it's "before it's done"?

What does this tell you?
std::cout << "End: g=" << g << std::endl;

> Also is that the correct way to find out the data size of the Struct,
Yes.
Was This Post Helpful? 0
  • +
  • -

#3 tolazytbh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 24-December 18

Re: Stuff after the for loops performing before it's done

Posted 19 January 2019 - 10:44 PM

It's apparently crashing on the variables it seems, do you know if it recreates the static variables every time the struct is created or is it like java where it's just one static method for every instance of it created?
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2295
  • View blog
  • Posts: 4,397
  • Joined: 30-May 10

Re: Stuff after the for loops performing before it's done

Posted 19 January 2019 - 11:14 PM

Your struct tile seems all wrong.
You pass in a tile (by value), then try to modify that local copy.
None of your member functions are going to have any effect on the outside world.

struct curScreenTile {
        RECT r;
        unsigned long rgb;
        bool activeTile = false;

        void setColor(int b, int g, int r) {
            rgb = ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
        }

        void setPos(short x, short y, short pixelSize) {
                r.left = x;
                r.top = y;
                r.right = y+pixelSize;
                r.bottom = x+pixelSize;
        }

        void setActive(bool toSet) {
            activeTile = toSet;
        }

        RECT getR() {
            return r;
        }

        unsigned long getColor() {
            return rgb;
        }

        bool active() {
            return activeTile;
        }
};




> It's apparently crashing on the variables it seems,
How about some hard evidence.
Like using a debugger to get a stack trace and some variable values.
Was This Post Helpful? 0
  • +
  • -

#5 tolazytbh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 24-December 18

Re: Stuff after the for loops performing before it's done

Posted 21 January 2019 - 12:17 AM

I did static so it doesn't recreate the getters/setters everytime a tile is created (there's going to be a quite a few (there's 1,000,000 pixels)


#0 0x7654949c strlen() (C:\WINDOWS\SysWOW64\msvcrt.dll:??)
#1 0x401b6c length(__s=<optimized out>) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/char_traits.h:263)
#2 ?? operator<< <std::char_traits<char> > (__s=<optimized out>, __out=...) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ostream:562)
#3 ?? Engine::run (this=<optimized out>) (C:\Users\Zach\Documents\c++\Learn\Engine.cpp:40)
#4 0x407330 execute_native_thread_routine () (??:??)
#5 ?? ?? () (??:??)

is the error i'm getting

This post has been edited by Skydiver: 21 January 2019 - 06:37 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#6 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2295
  • View blog
  • Posts: 4,397
  • Joined: 30-May 10

Re: Stuff after the for loops performing before it's done

Posted 21 January 2019 - 01:01 AM

> I did static so it doesn't recreate the getters/setters everytime a tile is created
You have a fundamental mis-understanding of how C++ works then.

Functions are NOT recreated every time you create an instance of an object.

These two things are essentially the same.
All C++ does for you here is automatically insert a 'this' pointer parameter, and use
the implied 'this' pointer parameter when accessing member variables. You could even use this->a if you really wanted to.
// C++
class foo {
  int a;
  public:
    void setvalue(int v) {
        a = v;
    }
};

// C
struct foo {
  int a;
};
void foo_setvalue(foo *this, int v) {
    this->a = v;
}


You can have your millions of objects, but there is only ever one function.
It just gets called millions of times with different 'this' pointers.

Further, short 1-liner setter/getter functions are most likely generated inline, so there is no actual function call overhead in using them.

Making a function static (inside the class) means you do NOT get a free 'this' pointer to play with.
That's why you keep having to supply a tile parameter.

But as I said, your tiles are passed by value.
static void setColor(int b, int g, int r, curScreenTile tile) {
    // tile is passed by value!
    // this assignment does NOT change the tile in the caller.
    tile.rgb = ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
}





#0 0x7654949c strlen() (C:\WINDOWS\SysWOW64\msvcrt.dll:??)
#1 0x401b6c length(__s=<optimized out>) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/char_traits.h:263)
#2 ?? operator<< <std::char_traits<char> > (__s=<optimized out>, __out=...) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ostream:562)
#3 ?? Engine::run (this=<optimized out>) (C:\Users\Zach\Documents\c++\Learn\Engine.cpp:40)
#4 0x407330 execute_native_thread_routine () (??:??)
#5 ?? ?? () (??:??)


> is the error i'm getting
OK, so what's at Engine.cpp line 40 that might be causing strlen() some grief?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: Stuff after the for loops performing before it's done

Posted 21 January 2019 - 06:40 AM

tolazytbh: There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area. If you really much quote, trim it down to just the specific item you want to address.
Was This Post Helpful? 0
  • +
  • -

#8 tolazytbh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 24-December 18

Re: Stuff after the for loops performing before it's done

Posted 23 January 2019 - 12:38 AM

View PostSalem_c, on 21 January 2019 - 01:01 AM, said:

> I did static so it doesn't recreate the getters/setters everytime a tile is created
You have a fundamental mis-understanding of how C++ works then.

Functions are NOT recreated every time you create an instance of an object.

These two things are essentially the same.
All C++ does for you here is automatically insert a 'this' pointer parameter, and use
the implied 'this' pointer parameter when accessing member variables. You could even use this->a if you really wanted to.
// C++
class foo {
  int a;
  public:
    void setvalue(int v) {
        a = v;
    }
};

// C
struct foo {
  int a;
};
void foo_setvalue(foo *this, int v) {
    this->a = v;
}


You can have your millions of objects, but there is only ever one function.
It just gets called millions of times with different 'this' pointers.

Further, short 1-liner setter/getter functions are most likely generated inline, so there is no actual function call overhead in using them.

Making a function static (inside the class) means you do NOT get a free 'this' pointer to play with.
That's why you keep having to supply a tile parameter.

But as I said, your tiles are passed by value.
static void setColor(int b, int g, int r, curScreenTile tile) {
    // tile is passed by value!
    // this assignment does NOT change the tile in the caller.
    tile.rgb = ((r & 0xff) << 16) + ((g & 0xff) << 8) + (b & 0xff);
}





#0 0x7654949c strlen() (C:\WINDOWS\SysWOW64\msvcrt.dll:??)
#1 0x401b6c length(__s=<optimized out>) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/bits/char_traits.h:263)
#2 ?? operator<< <std::char_traits<char> > (__s=<optimized out>, __out=...) (C:/Program Files (x86)/CodeBlocks/MinGW/lib/gcc/mingw32/5.1.0/include/c++/ostream:562)
#3 ?? Engine::run (this=<optimized out>) (C:\Users\Zach\Documents\c++\Learn\Engine.cpp:40)
#4 0x407330 execute_native_thread_routine () (??:??)
#5 ?? ?? () (??:??)


> is the error i'm getting
OK, so what's at Engine.cpp line 40 that might be causing strlen() some grief?


I went and googled Static c++ after reading this,

Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following, Static variable in functions.

I thought that was odd because idk what else static would be useful for... seems like it's exactly like java
So i'm going to go with that instead of just hoping it doesn't recreate each void every time a new struct is created


Line 40 is the beginning of the for-loop
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is online

  • member icon

Reputation: 5668
  • View blog
  • Posts: 17,402
  • Joined: 25-December 09

Re: Stuff after the for loops performing before it's done

Posted 23 January 2019 - 04:59 AM

Quote

I thought that was odd because idk what else static would be useful for... seems like it's exactly like java
So i'm going to go with that instead of just hoping it doesn't recreate each void every time a new struct is created

C++ is not Java and the meaning of the static keyword is not the same in these two languages.

First off in C++ the static keyword has different meaning depending on where it is being used.

When static is being used in a class with a member variable it means that that member variable will be the same variable for every instance of that class. When used with a class member function that function can only access static member variables, other static member functions, or functions from outside the class.

When static is being used with a normal function it means that that function has internal linkage and is only visible in the current compilation unit.

When static is being used with a variable inside some function are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime.

By the way do you realize that in C++ not everything must be part of a class?

Jim
Was This Post Helpful? 0
  • +
  • -

#10 tolazytbh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 24-December 18

Re: Stuff after the for loops performing before it's done

Posted 23 January 2019 - 08:48 AM

View Postjimblumberg, on 23 January 2019 - 04:59 AM, said:

Quote

I thought that was odd because idk what else static would be useful for... seems like it's exactly like java
So i'm going to go with that instead of just hoping it doesn't recreate each void every time a new struct is created

C++ is not Java and the meaning of the static keyword is not the same in these two languages.

First off in C++ the static keyword has different meaning depending on where it is being used.

When static is being used in a class with a member variable it means that that member variable will be the same variable for every instance of that class. When used with a class member function that function can only access static member variables, other static member functions, or functions from outside the class.

When static is being used with a normal function it means that that function has internal linkage and is only visible in the current compilation unit.

When static is being used with a variable inside some function are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime.

By the way do you realize that in C++ not everything must be part of a class?

Jim

Did you disagree to just disagree? I did what I highlighted in bold

is the underlined one a note you got from somewhere on something you did?

All of those go inline with what it is used for in java (@italian text)
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: Stuff after the for loops performing before it's done

Posted 23 January 2019 - 12:36 PM

We are just trying to make sure that you understand that declaring a class function as static doesn't really save you anything (significant). Regardless of whether the class function is static, or not, there is only one copy of the function. Our understanding of your previous posts is that you believe each instance of the class/strct will have its own copy of the function, and so you chose to use static to force only one copy.
Was This Post Helpful? 0
  • +
  • -

#12 tolazytbh   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 24-December 18

Re: Stuff after the for loops performing before it's done

Posted 23 January 2019 - 01:12 PM

Yeah, so it doesn't make a difference for memory with methods?

Also with the For loop, and the methods afterwords peforming before it's done, am I suppose to use something like malloc() to allocate the space for the million pixels + a color for each (doing a worst case scenario) (for the vector)

This post has been edited by tolazytbh: 23 January 2019 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: Stuff after the for loops performing before it's done

Posted 23 January 2019 - 03:01 PM

For non-virtual classes, it doesn't make difference.

Once a class has virtual functions, then each virtual function takes up one slot in the v-table. Each slot in the v-table is simply a pointer to that function. So again, the function only exists once. Most C++ programmers don't consider the cost of the v-table unless they hit a major performance bottleneck. To do so is premature optimization.

In C++, you use new and delete, not malloc() and free(). But that is if you want to manage the memory yourself. Simply use the std::vector<> and it'll take care of the memory management for you.
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg   User is online

  • member icon

Reputation: 5668
  • View blog
  • Posts: 17,402
  • Joined: 25-December 09

Re: Stuff after the for loops performing before it's done

Posted 23 January 2019 - 06:27 PM

Quote

Did you disagree to just disagree?

No, but you have, seems to me, contracted that dreaded selective reading disorder disease that seems to go around the programming community at times.

You seem to have missed several key points.

First in C++ there are multiple meanings of the static keyword that depend on where and how it is used.

Second C++, unlike Java can have functions and variables that are not part of any class.

So to illustrate:

A small complete C++ program that contains no classes.
Spoiler


Now a slightly larger program using the static keyword.
Spoiler


Now a C++ class that illustrates the use of the static keyword inside a class construct.
Spoiler



Jim
Was This Post Helpful? 2
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: Stuff after the for loops performing before it's done

Posted 23 January 2019 - 06:34 PM

And now finally for your question:

View Posttolazytbh, on 23 January 2019 - 03:12 PM, said:

Also with the For loop, and the methods afterwords peforming before it's done, am I suppose to ...

As stated above, you need to fix your code.

How did you determine that the code after the loop was being executed before the loop execution has completed? Do you have some minimal repro code to demonstrate what is happening?

For example, with this code:
#include <iostream>
#include <vector>

int main()
{
    const int NumberOfFlags = 10;
    std::vector<int> flags(NumberOfFlags);

    for (size_t i = 0; i < NumberOfFlags; i++)
        flags.at(i) = 1;

    std::cout << flags.at(NumberOfFlags - 1) << std::endl;

    return 0;
}

If your assertion that code after the for loop is executed before the for loop is done is correct, then line 12 should execute before the for loop on lines 9-10 completes. Since the array flags is filled with zeros and only changed to ones in the loop, then the output should be zero. With all my attempts to run the code, I always get one.

Can you show us some simple code where the code after loop is executed before the loop ends?
Was This Post Helpful? 1
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3