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

Author Topic: SFML- Thread  (Read 1054 times)

0 Members and 1 Guest are viewing this topic.

Ultralegendary

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
SFML- Thread
« on: December 06, 2020, 07:04:51 am »
hi, i have been working with SFML for 2 months.
I tried to do visualizing sorting methods.
After some research i found that threading is the only way which satisfy my needs.
But i failed to implement threading to my code. Got some errors, unexpected outputs.
Here is the code:
Code: [Select]
#include<SFML/Graphics.hpp>
#include<iostream>
#include<functional>

class sorting
{
     :// some code
     public:
          update_merge_sort(int a,int b)
          {
          // merge sort things
          // contains recursion function
          }

};

int main()
{
sorting objects;
sf::RenderWindow w(sf::VideoMode(1550, 850), "SORTING ALGORITHMS", sf::Style::Default);
sf::Thread thread_1(std::bind(&sorting::update_merge_sort,&objects,0,0));//declared global because the this has to run aslong as program runs, thread is not launched

sf::Event e;
while (w.isOpen()) {
while (w.pollEvent(e))
{
mos_pos = w.mapPixelToCoords(sf::Mouse::getPosition(w));
                        switch (e.type) {
                                 case 0:
                                           w.close(); break;
                                 case sf::Event::KeyPressed:
                                           if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                                           {       // start the thread here;
                                                    /*
                                                       thread_1(sf::Thread(std::bind(&sorting::update_merge_sort, &objects, 0, count - 1)));
                                                       thread_1=(sf::Thread(std::bind(&sorting::update_merge_sort, &objects, 0, count - 1)));
                                                       std::thread th(&sorting::update_merge_sort, &objects, 0, count - 1);

                                                        //sf::Thread ti((std::bind(&sorting::update_merge_sort, &objects, 0, count - 1)));

                                //thread_1(new t1);--
   
                                  thread_1.launch();


                                                   */
                                            }
                        }
              ://render things
           }
}



i cant find a way to start the thread when key is pressed.
I tried declaring thread inside IF statement, But the thread scope gets over there itself.
So i used sf::Thread, and tried calling the sf::Thread constructor to update the thread values, but failed.
I tried all possible ways mentioned which has been inside comments.

I Hope you can provide a way to solve this .




Kvaz1r

  • Newbie
  • *
  • Posts: 39
    • View Profile
    • Email
Re: SFML- Thread
« Reply #1 on: December 06, 2020, 11:04:43 am »
How your thread is supposed to work?
Start thread inside key pressed handler is no different from starting thread elsewhere:


        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type == sf::Event::KeyPressed &&
                event.key.code == sf::Keyboard::A
                )
            {
                std::thread t([]
                    {
                        std::cout << "Thread executing\n";
                    });

                t.join(); //wait for executing, joinable thread
            }
        }

 

anything