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

Author Topic: Pass paramater/argument's address to thread's function which is in class in SFML  (Read 4095 times)

0 Members and 1 Guest are viewing this topic.

rizaado

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Hello. I am wondering is this possible to pass parameter or argument`s address and get it with reference operator or with pointer in thread's function which is in class with SFML System Class. Can I send the value of argument too? I have read that is better to use C++ ISO 11. Is there something new with threads in C++ ISO 14?
Something like this:
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
class MyClass
{
public:

    void func(int & argument or int * argument or int argument)
    {
//make something with this argument for example ++argument or ++*argument or print it with cout  
    }
};
int main()
{
int argument = 0;

MyClass object;
sf::Thread thread(&MyClass::func, &object, (argument or &argument or argument  ));
thread.launch();
return 0;
}
 
Thank you. Have a nice day.
« Last Edit: November 01, 2014, 10:25:41 am by rizaado »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
I suggest you look into std::bind.
Though as you said, you should probably use std::thread you might want to look into std::function.

Btw. it's commonly referred to C++11 and C++14, since there's only the ISO standard.

And there doesn't seem to be something new specific for std::thread in C++14, why do you ask?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
You can pass any combination of arguments to std::thread -- by value, as a pointer or as a reference (through std::ref() and std::cref() wrappers). Check out the documentation for details.

Don't use sf::Thread if your compiler supports std::thread.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

rizaado

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
I am with windows 7, codeblocks, GNU GCC Compiler. I have seen that there are more people that have the same problem as mine. From codeblocks but I can`t turn on C++11. I checked it true from Settings->Compiler and from project->Build Options but it messages:

thread is not member of std

This is for example the code:
// thread example
#include <iostream>       // std::cout
#include <thread>         // std::thread
 
void foo()
{
  // do stuff...
}

void bar(int x)
{
  // do stuff...
}

int main()
{
  std::thread first (foo);     // spawn new thread that calls foo()
  std::thread second (bar,0);  // spawn new thread that calls bar(0)

  std::cout << "main, foo and bar now execute concurrently...\n";

  // synchronize threads:
  first.join();                // pauses until first finishes
  second.join();               // pauses until second finishes

  std::cout << "foo and bar completed.\n";

  return 0;
}

The administrator of codeblocks` forum said and you can see more here - http://forums.codeblocks.org/index.php?topic=17838.0;prev_next=prev:
Quote
We do not build vompilers just an IDE, that works with many compilers.
We ship with a TDM's gcc on windows to make it easy to start coding for beginners (and most other users) without the need to explicitely download a compiler.
So you as user are free to use any supported compiler, with or without std:thread-support.

He answered to someone who said:
Quote
oh my bad Sad that's really freaking. Ok I have than to swap to Linux partition.
is there any chance to add support for std::thread ever in GCC at this point?

What can I do to change my compiler or?
« Last Edit: November 02, 2014, 10:02:21 am by rizaado »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
You download it. And in Options > Compiler (or something) you set the path to the downloaded compiler instead of the built-in one. It's as simple as that.
Laurent Gomila - SFML developer

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
The thread in codeblocks forum you linked is more than one and a half years old, back then TDM 4.7.1 may have not supported std::thread, but 4.8.1 does: http://tdm-gcc.tdragon.net/ . That also suggests you are still using outdated codeblocks 12.11, when the newer 13.12 is better and can be got with an included TDM 4.8.1.

You should not use the compiler menu for setting build options, because that setting is only for that computer and will be forgotten if you copy your project to another. Open your project instead and rightclick on it and choose build options, to get a similar menu that works for saving the options in the project file, but dont forget to set it for both Debug and Release.
Also they do not add all GCC options to the code::blocks menu and when you have an old version you may need to set the older -std=c++0x instead of -std=c++11 (or type it into Other Options yourself).

Btw., there is an option in the compiler menu/Other Settings to show the full command line, activate it and see for yourself what gets passed to g++!

rizaado

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Thank you wintertime and everybody who wrote it this topic. I reinstalled the whole IDE with TDM-GCC-481 and it worked.