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

Author Topic: SFML + V8 + Threads - How to?  (Read 3898 times)

0 Members and 2 Guests are viewing this topic.

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
SFML + V8 + Threads - How to?
« on: September 17, 2012, 06:58:16 am »
Hi,

I made a simple thread example using SFML, V8 and SF::THREAD.

The problem is when the thread is executed, my program quit with message "segmentation fault: 11".

I only execute the same method to read a JS and execute it, but on another method by thread.

You can see example here:
https://github.com/prsolucoes/sfml-cmake

To use thread method, change var:
Quote
bool useThread = false;

to:
Quote
bool useThread = true;

In "main.cpp" file.

The method that thread call is the same of main loop code without thread:

Quote
void start()
{
    v8::HandleScope handleScope;
    v8::Handle<v8::String> source = readFile(resourcePath() + "/js/robot1.js");
    v8::Handle<v8::Script> script = v8::Script::Compile(source);
    v8::Handle<v8::Value> result = script->Run();
}

Thanks for any help.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML + V8 + Threads - How to?
« Reply #1 on: September 17, 2012, 07:45:45 am »
Sorry, but nobody seems to be able to help you with these problems, which are most likely related to V8 rather than SFML.

You should avoid opening a new thread everyday (especially if you solve your problem yourself the next day ;)).
Laurent Gomila - SFML developer

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: SFML + V8 + Threads - How to?
« Reply #2 on: September 17, 2012, 04:09:28 pm »
When i open, is because i dont know.

And after i discover the problem, i share here to others that have the same problem see and learn too.

sorry.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML + V8 + Threads - How to?
« Reply #3 on: September 17, 2012, 04:20:23 pm »
It's nice to share, but it's V8 stuff and has nothing to do with SFML, so it's a little off-topic here ;)
Laurent Gomila - SFML developer

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: SFML + V8 + Threads - How to?
« Reply #4 on: September 17, 2012, 07:59:50 pm »
Hi Laurent,

Now it is running with threads(already commited working version), but it is so slowly, and i dont know if it is because v8 or sfml or anything process that is locking the app.

If it is on thread, the main thread dont stop and if you see on log, it print the message "::start" (this is printed  by start method) too slow. It is not on thread, because the main thread wait it finish to call again. If threads working correct, the main loop is create too many threads and we will see some "::start" on log.

Agree?

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: SFML + V8 + Threads - How to?
« Reply #5 on: September 17, 2012, 11:44:42 pm »
V8 only allows one thread to access the context at the same time so you can never get true paralellism with V8.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: SFML + V8 + Threads - How to?
« Reply #6 on: September 18, 2012, 12:12:10 am »
Hum..

I refactored the code and now it is organized.

The problem now is two:

1 - The sprites are rendered with problem
2 - On each main loop we have a lag

[screenshot attached]


@Groovy

But i can load and execute it on a context that runs only in the thread? Because i only need execute it to the scripts modify the object. So everything can be in a thread. It is true?

[attachment deleted by admin]
« Last Edit: September 18, 2012, 12:14:10 am by prchakal »

prchakal

  • Full Member
  • ***
  • Posts: 142
    • View Profile
Re: SFML + V8 + Threads - How to?
« Reply #7 on: September 18, 2012, 02:57:56 am »
The problem of lag i solve, sleep function is waiting 1sec and not 1 ms, but the sfml is rendering so strange the images and nothing was changed about it.


Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: SFML + V8 + Threads - How to?
« Reply #8 on: September 18, 2012, 07:36:36 pm »
Are thread 1 modifying the data while thread 2 is trying to read it? For instance a thumb of rule would be to have all graphics(that means like everything concerning SFML) in one thread and your logics in another(scripts, etc, etc whatever).

Because if you are modifying let's say a sprite in thread 1 and then try to render it in thread 2, it wouldn't suprise me if it looked weird. Even worse would be if you were rendering with both threads.

Also think of with V8, if you create a javascript object that encapsulates a Sprite in thread 1, all scripts are run in this thread and so on, everybody is happy. But if that sprite is used in another thread then there will of course also be a problem. Threads gives more of a headache than they are worth so you know. Especially if you are making a 2D game.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio