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

Author Topic: Passing variable to external thread class  (Read 4137 times)

0 Members and 1 Guest are viewing this topic.

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
Passing variable to external thread class
« on: April 24, 2012, 11:44:41 pm »
How would the procedure work in the tutorials for an external thread class (SFML 2.0), like the code below:

Thread.ccp
Code: [Select]
#include "globalInclude.h"

class ThreadClass{
public:
void run(){
//NEED to use a variable that is passed from introductionState.cpp
}

private:
sf::Uint8 mAlpha;
};

introductionState.ccp
Code: [Select]

//VARIABLE TO BE PASSED
sf::uint8 alpha

...
...

void IntroScreen::TransitionOut(){

ThreadClass threadc;
sf::Thread th(&ThreadClass::run,&threadc);
th.launch();
transition=true;
}

Best regards
« Last Edit: April 24, 2012, 11:47:10 pm by snoozer87 »

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Passing variable to external thread class
« Reply #1 on: April 25, 2012, 12:29:32 am »
Alright, I got it running but my thread is blocking the caller and so the caller waits on the thread to be finished, which makes it not a thread at all :p.

Caller (does the sfml drawing and stuff) introScreen.cpp
Code: [Select]
void IntroScreen::TransitionOut(){
if(!transition){
transition=true;
ThreadClass threadc(this);
sf::Thread th(&ThreadClass::run,&threadc);
th.launch();
}
}
void IntroScreen::setFadeAmount(sf::Uint8 alpha){
mfade_alpha=alpha;
}

Thread.h
Code: [Select]
#include "globalInclude.h"
#include "GameScreen.h"
class ThreadClass{
public:
ThreadClass(GameScreen* pScreen);
ThreadClass();
void run();

private:
sf::Uint8 mAlpha;
sf::Clock mFadeClock;
GameScreen* m_pScreen;
};

Thread.ccp
Code: [Select]
#include "ThreadClass.h"

ThreadClass::ThreadClass(GameScreen* pScreen){
m_pScreen=pScreen;
mAlpha=0;
}

ThreadClass::ThreadClass(){
mAlpha=0;
}

void ThreadClass::run(){
bool done=false;
while(!done){
if(mAlpha!=255){
if(mFadeClock.getElapsedTime().asMilliseconds()>500){
if((255-mAlpha)<60){
mAlpha=255;
done=true;
}else{
mAlpha+=60;
}
//m_pScreen->setFadeAmount(mAlpha);
mFadeClock.restart();
}
}else{
done=true;
}
}
}

Is this the right way to do it and if so, what am I doing wrong?

Best regards

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Passing variable to external thread class
« Reply #2 on: April 25, 2012, 08:05:05 am »
Your thread object is local to a function, so it is destroyed (and the main thread waits for it) immediately after being created.
Laurent Gomila - SFML developer

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Passing variable to external thread class
« Reply #3 on: April 25, 2012, 09:19:17 am »
I've changed them to global variables, but for some reason it works in VS2010 release mode, but when I try the .Exe it doesn't work. I should clarify that the not working is the fact that the thread doesn't seem to start and thus the screen is not faded.

Code: [Select]
//Global variables
ThreadClass threadc(IntroScreen::Instance());
sf::Thread th(&ThreadClass::run,&threadc);
« Last Edit: April 25, 2012, 09:28:47 am by snoozer87 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Passing variable to external thread class
« Reply #4 on: April 25, 2012, 09:29:41 am »
Avoid global variables, especially those that do something in their constructor and/or destructor (don't forget that the corresponding code will be executed before/after main() runs).
Laurent Gomila - SFML developer

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Passing variable to external thread class
« Reply #5 on: April 25, 2012, 09:34:03 am »
Alright, sry for bothering you so much, but how would you do it then. Because now I tried this and the exe still does nothing:

caller.h
Code: [Select]
...
private:
ThreadClass *threadc;
sf::Thread *th;

caller.cpp
Code: [Select]
void IntroScreen::Init(sf::RenderWindow* SFMLView1){
...
threadc=new ThreadClass(this);
th=new sf::Thread(&ThreadClass::run,threadc);
...
}

void IntroScreen::TransitionOut(){
if(!transition){
transition=true;
th->launch();
}
}

**EDIT

MY apologies, I forgot to add my fade.png to the release map :s. But is my code correct, or is there a cleaner and better way to do it?
« Last Edit: April 25, 2012, 09:43:02 am by snoozer87 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Passing variable to external thread class
« Reply #6 on: April 25, 2012, 09:41:30 am »
Your code is ok ;)
Laurent Gomila - SFML developer