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

Author Topic: Events outside of Main Thread  (Read 2222 times)

0 Members and 1 Guest are viewing this topic.

Mos_Fett

  • Newbie
  • *
  • Posts: 2
    • View Profile
Events outside of Main Thread
« on: October 01, 2017, 09:25:48 pm »
I'm making a cross platform C++ graphing library using SFML. The end goal is to make it so you can quickly pass two arrays of numbers to an object and have them plotted. I've got most of the plotting code working but I also want to handle mouse events so you can scroll around on the plot, but I don't want to put the event loop in the main thread because I want the plots to respond to events while other things are going on in the main loop. For example, this is the kind of program I expect to use as an actual application of my code:

int main(){
    int x[] = {1, 2, 3, 4, 5};
    int y[] = {2, 4, 6, 8, 10};
    MyPlot p(x, y); //Plot displays on screen, mouse can scroll around on window

    /*
    Lengthy slow calculation solving all of science goes here...
    */


    int x2[] = {1, 2, 3, 4, 5};
    int y2[] = {42, 42, 42, 42, 42};
    p.update(x2, y2); //Plot is updated

   p.loop_until_closed(); //Plot remains on screen until window is closed
}

After reading around on the forum and docs it looks like there isn't a way to handle events outside the main thread, but I want to make sure this is true before I change my program so you have to wait to use your nice pretty charts until you can sacrifice the main loop to event handling.

In summary:

1. Is there a way to handle events outside the main thread? (I've tried and been told I can't create a window or handle events from a worker thread)
2. Do you know of any work around so my window can be responsive while the main thread is left open for other (non-event handling) duties?

Thanks in advance  :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Events outside of Main Thread
« Reply #1 on: October 01, 2017, 11:36:32 pm »
Not sure what sources you read these things on. The important part is to poll events on the same thread that the window was created on (see the remark in the tutorial).

The thing with multi-threading is, that if you're not familiar with the topic, most will assume it's pretty easy, but making sure that shared resources are synced correctly and that there aren't any race conditions, is often quite a hard task and it's very easy to introduce bad bugs.
So make sure you actually have the required knowledge in parallel programming before you try to tackle this task.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mos_Fett

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Events outside of Main Thread
« Reply #2 on: October 02, 2017, 04:59:48 am »
Gotcha, I didn't see that they explicitly said you can't create a new window in a worker thread on OSX (the platform I'm developing on). This (https://en.sfml-dev.org/forums/index.php?topic=2919.msg19099) forum post had me wondering if it was possible because the last post mentioned creating the window from outside the main thread.

I will heed your cautions with multithreading too. I've worked with multithreaded programs (even coding directly on GPUs a little bit) in the past, but you can never be too careful!

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Events outside of Main Thread
« Reply #3 on: October 02, 2017, 04:47:49 pm »
Can you not keep the Window creation and event polling in the main thread and move your slow calculations to the other thread?

 

anything