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

Author Topic: Can't figure out how to work with std::thread and SFML  (Read 6639 times)

0 Members and 1 Guest are viewing this topic.

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
Can't figure out how to work with std::thread and SFML
« on: May 30, 2013, 07:14:13 pm »
In a previous thread I asked for some help and was told to use std::thread.

I basically declared a function:

void initialize(sf::RenderWindow &winMain);

...

int main()
{
    sf::RenderWindow winMain;
    std::thread loadingThread(initialize, winMain);
 
     ...
 
     loadingThread.join();
}

But it doesn't work. I just want it to run a thread with a function that needs my renderwindow passed as a paramater to it. The error:

Error   1   error C2661: 'std::thread::thread' : no overloaded function takes 2 arguments

What could I be doing wrong? I'm just confused. I'm sorry I couldn't show the full source code, but it would just confuse you further.

If something is fundamentally wrong, please tell me, how would I make a thread that uses a function declared in the same file which has a pointer paramater?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't figure out how to work with std::thread and SFML
« Reply #1 on: May 30, 2013, 08:15:50 pm »
You would have to wrap winMain into a std::ref so that it is passed by reference and not copied internally by the thread, but according to the error message this is not your problem. What's your compiler?
Laurent Gomila - SFML developer

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Can't figure out how to work with std::thread and SFML
« Reply #2 on: May 31, 2013, 07:31:57 am »
You would have to wrap winMain into a std::ref so that it is passed by reference and not copied internally by the thread, but according to the error message this is not your problem. What's your compiler?
Latest version of mingw, it should work fine

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't figure out how to work with std::thread and SFML
« Reply #3 on: May 31, 2013, 07:53:34 am »
Can you show a complete and minimal code that reproduces this problem, instead of isolated fragments?
Laurent Gomila - SFML developer

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Can't figure out how to work with std::thread and SFML
« Reply #4 on: May 31, 2013, 09:51:26 am »
Can you show a complete and minimal code that reproduces this problem, instead of isolated fragments?

The code is very confusing (I am not a clean code person), but this is basically the structure:

Game.cpp

Game mygame;

void loadWorld(sf::RenderWindow &winMain)
{
      // some stuff gets initialized here, I need the renderwindow paramater to calculate some camera bounds
}

// handle input and render the stuff here.

int main()
{
        sf::RenderWindow winMain;
        std::thread loadingThread(loadWorld, winMain);

        winMain.setSize(sf::Vector2u(SCREEN_W, SCREEN_H));
        winMain.setTitle("My game");
        mygame.paused = false;

        sf::Image icon;
        sf::Clock clock;

        LoadingScreen = new loadingScreen();
        GameState = new gameState(LOADING_SCREEN);
       
        icon.loadFromFile("resources\\icon.png");
        winMain.setIcon(50, 50, icon.getPixelsPtr());
        winMain.setFramerateLimit(60);
        LoadingScreen -> loadingContext = IN_GAME;

        loadingThread.join();

        while(winMain.isOpen())
        {
                sf::Time dt = clock.restart();
                mygame.handleInput(winMain);
                mygame.gameLogic(winMain);
        }
}

The game is basically a class, to encapsulate everything and keep the main function isolated. Everything worked fine when I used a thread before, but main was in its own file, and the renderwindow was global so I was calling extern sf::RenderWindow winMain instead of just passing it as an argument in my methods.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't figure out how to work with std::thread and SFML
« Reply #5 on: May 31, 2013, 10:21:40 am »
This code is not complete nor minimal. Please read this: http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368

A typical minimal example for this kind of error would be the following:

#include <thread>

void f(int) {}

int main()
{
    std::thread t(f, 5);
    return 0;
}
Laurent Gomila - SFML developer

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Can't figure out how to work with std::thread and SFML
« Reply #6 on: May 31, 2013, 11:35:03 am »
This code is not complete nor minimal. Please read this: http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368

A typical minimal example for this kind of error would be the following:

#include <thread>

void f(int) {}

int main()
{
    std::thread t(f, 5);
    return 0;
}

Well it's not complete because the rest doesn't matter, it's just a bunch of functions for handling input and game logic, and since it's a large game it would take up a lot of useless space, it's the only code that can be troubled, the rest has absolutely no issues which is why I didn't want to bother you with it. Here's another attempt, though the code above basically contains the same thing. Also I can't post the whole source code to make the code runable, I have 14 files, and using all of them in the same file where this problem occurs. This is basically the only line of code that can possibly cause this issue.

#include <functional>
#include <thread>

void loadWorld(sf::RenderWindow &winMain) {};

int main()
{
        sf::RenderWindow winMain;
        std::thread loadingThread(loadWorld, winMain);

        winMain.setSize(sf::Vector2u(SCREEN_W, SCREEN_H));
        winMain.setTitle("Council of Torment");
       
        icon.loadFromFile("resources\\icon.png");
        winMain.setIcon(50, 50, icon.getPixelsPtr());
        winMain.setFramerateLimit(60);

        loadingThread.join();
}

I'm sorry if this wasn't good either, but it's the only piece of code that can actually have an error, there's no point in showing anything more or less, because it's just two lines of code, which cause the error. Replacing std::Thread with sf::Thread would probably fix it, since that's what I've been using up to the point when I heard std::thread is better.

But supposing the code is not good, how would you make a thread that runs my kind of function?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't figure out how to work with std::thread and SFML
« Reply #7 on: May 31, 2013, 12:24:48 pm »
You don't understand the purpose of a complete and minimal code. We don't care about your initial project, so don't try to keep parts of it, just write a new code sample that focus on the actual problem (in your case: a compiler error with a thread taking one argument). That's what I did.

Your code is not minimal, we don't care at all about the title or icon of your window, it won't change anything to the problem.

And of course it needs to be complete so that I can test it quickly. We don't want to spend hours to try to fix it, just to be able to run it.

But all these points are explained in the link I gave you previously, have you read it?

Anyway, does my minimal code reproduce the error or not?
Laurent Gomila - SFML developer

Chunker120

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: Can't figure out how to work with std::thread and SFML
« Reply #8 on: May 31, 2013, 05:48:30 pm »
You don't understand the purpose of a complete and minimal code. We don't care about your initial project, so don't try to keep parts of it, just write a new code sample that focus on the actual problem (in your case: a compiler error with a thread taking one argument). That's what I did.

Your code is not minimal, we don't care at all about the title or icon of your window, it won't change anything to the problem.

And of course it needs to be complete so that I can test it quickly. We don't want to spend hours to try to fix it, just to be able to run it.

But all these points are explained in the link I gave you previously, have you read it?

Anyway, does my minimal code reproduce the error or not?

I've read it but didn't quite understand, after all if there's an error I can't supply code that runs. Anyhow, I get what you meant now. I tried running your minimal code and it all works fine, here is a sample code that doesn't work at all, but is supposed to.

#include <SFML/Graphics.hpp>
#include <thread>

void f(sf::RenderWindow &winMain) {}

int main()
{
    sf::RenderWindow winMain();
    std::thread t(f, winMain);
    return 0;
}

All I want to do is create a thread based on a function that has a reference to my renderwindow. I never used std::Thread so I have no idea how to use it. I really don't know how to supply more minimal code because I don't know how to make it work.

Also I should mention that this code shows a different error:

void (sf::RenderWindow &)' : cannot convert parameter 1 from 'sf::RenderWindow (__cdecl *)(void)' to 'sf::RenderWindow &
« Last Edit: May 31, 2013, 05:50:15 pm by Chunker120 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't figure out how to work with std::thread and SFML
« Reply #9 on: May 31, 2013, 07:13:17 pm »
That's because sf::RenderWindow winMain(); declares a function, not a variable. Remove the parenthesis.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Can't figure out how to work with std::thread and SFML
« Reply #10 on: June 01, 2013, 12:11:38 pm »
You also need std::ref().
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything