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

Author Topic: Linux Threading Issue  (Read 27958 times)

0 Members and 2 Guests are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Linux Threading Issue
« Reply #15 on: December 10, 2011, 12:53:07 pm »
Sometimes XInitThreads() must be called to make X aware that it's running in a multi-threaded application, but it shouldn't be necessary here.
Laurent Gomila - SFML developer

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Linux Threading Issue
« Reply #16 on: December 11, 2011, 01:01:45 am »
How exactly do I call XInitThreads()...I assume I need to include something though I don't have the slightest clue what, and I couldn't find it with a Google search.
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Linux Threading Issue
« Reply #17 on: December 11, 2011, 08:15:27 am »
You must include <X11/Xlib.h> (and of course this code is for Linux only).
Laurent Gomila - SFML developer

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Linux Threading Issue
« Reply #18 on: December 13, 2011, 02:05:02 pm »
Thanks :D
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Linux Threading Issue
« Reply #19 on: December 13, 2011, 02:07:20 pm »
Did it solve your problem?
Laurent Gomila - SFML developer

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Linux Threading Issue
« Reply #20 on: February 17, 2012, 08:16:39 am »
Okay, I know this reply is a little late, but it did not. I have managed to narrow down the issue a bit though.

This code gives the threading error (during run-time) described on the previous page.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

#define WIDTH 800
#define HEIGHT 600

void display(void *UserData)
{
   sf::RenderWindow* App = static_cast<sf::RenderWindow*>(UserData);
    sf::Image screen(WIDTH, HEIGHT);
    sf::Sprite sprite;
    while (1)
    {
        App->Clear();
        App->Draw(sprite);
    }
}

int main()
{
    sf::RenderWindow App(sf::VideoMode(WIDTH, HEIGHT, 32), "ElectroToy");
    sf::Thread graphics(&display, &App);
    graphics.Launch();
    App.SetActive();
    while (1)
        App.Display();
}


However, if you remove all statements that do anything to App other than App.SetActive(false), it works.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

#define WIDTH 800
#define HEIGHT 600

void display(void *UserData)
{
   sf::RenderWindow* App = static_cast<sf::RenderWindow*>(UserData);
    sf::Image screen(WIDTH, HEIGHT);
    sf::Sprite sprite;
    while (1)
    {
        App->Clear();
        App->Draw(sprite);
        App->Display();
    }
}

int main()
{
    sf::RenderWindow App(sf::VideoMode(WIDTH, HEIGHT, 32), "ElectroToy");
    sf::Thread graphics(&display, &App);
    graphics.Launch();
    App.SetActive();
    graphics.Wait();
}


Now, my main application I'm working on (which is just the above further on in development) will also simply supply this error if calls to App happen in the main function as well as another one.Though at the moment, I have got rid of all App calls from the main function into another function in the same thread. This seems to execute a random number of times (usually, somewhere around 40 times) before showing the threading error. (The threading error only shows in Debug configuration) I am still trying to narrow down the cause of this and the current code is a little too long to post on the forums but if you want to see it, I'll post it. (It's just a bit of a mess).

Thanks for any help
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Linux Threading Issue
« Reply #21 on: February 17, 2012, 08:42:47 am »
Your first code is not supposed to work: a window can only be active in one thread at the same time. Note that calling Clear/Draw/Display implicitely tries to activate the window.

Your second code works as expected, all the graphics calls are done in the same thread. Except that SetActive() should be SetActive(false) in the main thread.
Laurent Gomila - SFML developer

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Linux Threading Issue
« Reply #22 on: February 17, 2012, 10:12:33 am »
That explains everything...thanks a bunch
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Linux Threading Issue
« Reply #23 on: February 17, 2012, 10:55:23 am »
Actually, I think I need help on just one other small thing that isn't quite worth making a new thread about. What would the best way of getting events from the "graphics" thread into the main thread. I frankly, cannot see how to do this without tossing huge amounts of data between threads and even then, I'm not quite sure what to do.

Thanks a lot
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Linux Threading Issue
« Reply #24 on: February 17, 2012, 11:03:23 am »
Which events? The window's ones (sf::Event)?
Laurent Gomila - SFML developer

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Linux Threading Issue
« Reply #25 on: February 17, 2012, 11:12:30 am »
Yes, I should have said. the

Code: [Select]
while (App.GetEvent(Event))

bit is what I need to get out of the that thread. Of course, I could just put it in the other thread but then I don't see how to access it as I can't think of a way to pass a pointer's address from the thread to the main function
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Linux Threading Issue
« Reply #26 on: February 17, 2012, 11:25:22 am »
GetEvent must be called in the same thread where the window was created, that's a limitation of some OSes. If I remember correctly, on OS X, windows and events are even restricted to the main thread.
Laurent Gomila - SFML developer

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Linux Threading Issue
« Reply #27 on: February 17, 2012, 01:08:29 pm »
So, I have to put all the display stuff and event handling stuff in the main thread and all the rest in different threads.

Thanks, as usual, you're a great help
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Linux Threading Issue
« Reply #28 on: February 17, 2012, 01:11:43 pm »
Quote
So, I have to put all the display stuff and event handling stuff in the main thread and all the rest in different threads

There's no restriction on the "display stuff" -- whatever you put in this expression.
Laurent Gomila - SFML developer