Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can only access global variables in my thread  (Read 4875 times)

0 Members and 1 Guest are viewing this topic.

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Can only access global variables in my thread
« on: November 07, 2013, 11:30:00 pm »
Sf::Thread can only access global variables in my project, but i think that should not be?
void RenderChunks();



In this example loadscreen can only be used if its declared out of main function.


int main(int argc, char **argv)
{
      // create a thread with RenderChunks() as entry point
      sf::Thread ChunkRenderThread(&RenderChunks);
      // run it
      ChunkRenderThread.launch();
return 1;
}
void RenderChunks()
{
   loadscreen = true;
   std::cout << "thread"<< std::endl;
   loadscreen = false;
}

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Can only access global variables in my thread
« Reply #1 on: November 07, 2013, 11:37:49 pm »
What kind of programming background do you have (if any) prior to C++? Because in almost all strongly-typed languages with a concept of automatic lifetime management (scopes, etc.) that I have seen, this is not possible. You declare an object in a scope and you use that object within that scope. Objects aren't implicitly shared between different scopes, hence you can't declare something in your main() and expect to be able to use it somewhere else. The global scope is of course the "super-scope" if you will, and all objects declared within it are naturally accessible everywhere where its declaration has effect. This is really really basic programming knowledge (not even specific to C++). If you have difficulties understanding this, you should try to learn a bit more about it before continuing.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Can only access global variables in my thread
« Reply #2 on: November 07, 2013, 11:47:34 pm »
Ofc i know this lol, i was already wondering xD

I am just referring to the sf::Thread tutorial on the sfml site which says that it is possible.

->All the threads of a program share the same memory, they can access all the variables of the program.

This discription is just a bit confusing formulated then in combination with the example of mutex´es
« Last Edit: November 07, 2013, 11:51:42 pm by etixpp »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Can only access global variables in my thread
« Reply #3 on: November 07, 2013, 11:51:01 pm »
Can you quote the part that says this is possible? Because I can't find it...

Edit: That is just an explanation of what happens at the hardware level. Variable access is still constrained by the language. Of course if you hardcoded variables to be stored at specific addresses you could use them without an explicit name from all threads just by address, but sf::Thread doesn't magically override any language specifications. Any library written to conform to the language standard will never override any of its specifications. This is also common sense...
« Last Edit: November 07, 2013, 11:55:04 pm by binary1248 »
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Can only access global variables in my thread
« Reply #4 on: November 07, 2013, 11:54:02 pm »
Can you quote the part that says this is possible? Because I can't find it...

http://www.sfml-dev.org/tutorials/2.1/system-thread.php

At the subheadline "Protecting shared data" first sentence.



AlexAUT

etixpp

  • Jr. Member
  • **
  • Posts: 82
    • View Profile
    • FoxFire Development Website
    • Email
Re: Can only access global variables in my thread
« Reply #5 on: November 07, 2013, 11:58:49 pm »
ye, sry, i was just a bit confused about this, can be closed then^^

 

anything