Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/update tutorial about threads #177

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<li><a href="#system-mutex">How do I use sf::Mutex?</a></li>
<li><a href="#system-thread-container">Why can't I store my sf::Thread in an STL container?</a></li>
<li><a href="#system-sleep">Why doesn't sf::sleep sleep for the amount of time I want it to?</a></li>
<li><a href="#system-thread-safe">Is SFML thread-safe?</a></li>
</ul>

<p><strong><a href="#programming">Programming in General</a></strong></p>
Expand Down Expand Up @@ -588,6 +589,10 @@
<p>One thing to remember is that although the operating system marks your thread as "awake" after it is done sleeping, even for exactly the correct duration, it doesn't mean it resumes execution immediately. It could have just missed the moment at which the scheduler selects which task to execute next and thus must wait for the next transition. In this case, although your thread slept for the correct amount, it will appear to you as if it slept for more. SFML doesn't allow you to sleep for 0 duration, however if you could, you would notice that it in fact takes a slight bit of time as well. This is because it is common for specifying 0 to the operating system to translate to simply yielding your execution time slice to another thread.</p>
<p>In the end, what this means is that <code>sf::sleep()</code> is merely a guideline, and not a contract. The longer you sleep for, the more accurate it will be. The shorter you sleep for, the less accurate it will be and to a certain extent more dependent on luck it will become.</p>

<h3 id="system-thread-safe"><a class="h3-link" href="#system-thread-safe">Is SFML thread-safe?</a><a class="back-to-top" href="#top" title="Top of the page"></a></h3>
<p>No, SFML objects are not thread-safe by themselves. If you need thread-safety you must protect them. SFML comes with <a href="/documentation/latest/classsf_1_1Mutex.php"><code>sf::Mutex</code></a> and <a href="/documentation/latest/classsf_1_1Lock.php"><code>sf::Lock</code></a> for this purpose but you can use other tools you may have available such as C++11's <code>std::mutex</code> and <code>std::lock_guard</code> as you see fit.</p>
<p>For more information on how to protect you shared data, refer to the <a href="/tutorials/latest/system-thread.php#protecting-shared-data">official documentation</a>.</p>

<h2 id="programming"><a class="h2-link" href="#programming">Programming in General</a><a class="back-to-top" href="#top" title="Top of the page"></a></h2>

<h3 id="prog-raii"><a class="h3-link" href="#prog-raii">What is RAII and why does it rock?</a><a class="back-to-top" href="#top" title="Top of the page"></a></h3>
Expand Down
12 changes: 10 additions & 2 deletions tutorials/2.6/graphics-draw-fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,19 @@
}
</code></pre>
<p>
Comme vous pouvez le voir, vous n'avez même pas besoin d'activer la fenêtre dans le thread de dessin, SFML le fait automatiquement pour vous dès que nécessaire.
Remarque : vous n'avez pas besoin d'activer la fenêtre dans le thread de dessin, SFML le fait automatiquement pour vous dès que nécessaire à condition
que le contexte OpenGL ait été correctement désactivé auparavant.
</p>
<p>
Souvenez-vous : il faut toujours créer la fenêtre et gérer ses évènements dans le thread principal, pour un maximum de portabilité, comme expliqué dans le
<a href="./window-window.php" title="Tutoriel sur les fenêtres">tutoriel sur les fenêtres</a>.
<a href="./window-window-fr.php#choses-ce-savoir-ce-propos-des-fencotres" title="Tutoriel sur les fenêtres">tutoriel sur les fenêtres</a>.
</p>
<p class="important">
L'exemple présentée ici n'est pas complètement thread-safe car un thread pourrait fermer la fenêtre pendant que l'autre l'utilise encore.
De manière générale, les objets SFML ne sont pas eux-mêmes thread-safes et vous devez vous-mêmes
<a href="./system-thread-fr.php#protceger-les-donncees-partagcees" title="Protéger les données partagées">protéger les données partagées</a>.<br/>
Notez que vous pouvez dessiner et gérer les évènements d'une fenêtre dans deux threads différents sans soucis car ces
deux fonctionnalités sont complètement indépendantes.
</p>

<?php
Expand Down
12 changes: 10 additions & 2 deletions tutorials/2.6/graphics-draw.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,19 @@ function.
}
</code></pre>
<p>
As you can see, you don't even need to bother with the activation of the window in the rendering thread, SFML does it automatically for you whenever it needs to be done.
Note that you don't need to activate the window in the rendering thread, SFML does it automatically for you whenever it needs
to be done, as long as the OpenGL context is properly deactivate beforehand.
</p>
<p>
Remember to always create the window and handle its events in the main thread for maximum portability. This is explained in the
<a href="./window-window.php" title="Window tutorial">window tutorial</a>.
<a href="./window-window.php#things-to-know-about-windows" title="Window tutorial">window tutorial</a>.
</p>
<p class="important">
The example shown here is not completely thread-safe because a thread could close the window while another still uses it.
Generally speaking, SFML objects are not themselves thread-safe and you need to
<a href="./system-thread.php#protecting-shared-data" title="Protecting shared data">protect the shared data</a> yourself.<br/>
Note that you can draw and handle the events of a window in two different threads without issues because these
two functionalities are independent.
</p>

<?php
Expand Down
3 changes: 3 additions & 0 deletions tutorials/2.6/system-thread-fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@
plusieurs threads en même temps. Et si l'opération en question n'est pas <em>thread-safe</em>, le résultat sera indéterminé (c'est-à-dire que
cela pourrait planter ou corrompre des données).
</p>
<p class="important">
Les objets SFML ne sont en général pas thread-safe. Vous devez les protéger si vous les utilisez dans plusieurs threads en même temps !
</p>
<p>
Il existe plusieurs outils de programmation pour vous aider à protéger les variables partagées et rendre votre code thread-safe, ils sont
appelés "primitives de synchronisation". Les plus communs sont les mutexs, les sémaphores, les conditions d'attente et les <em>spin locks</em>.
Expand Down
3 changes: 3 additions & 0 deletions tutorials/2.6/system-thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ classes are a good solution.
since threads run in parallel, it means that a variable or function might be used concurrently from several threads at the same time.
If the operation is not <em>thread-safe</em>, it can lead to undefined behavior (ie. it might crash or corrupt data).
</p>
<p class="important">
SFML objects are usually not thread-safe. You must protect them to use them in multiple threads at the same time!
</p>
<p>
Several programming tools exist to help you protect shared data and make your code thread-safe, these are called synchronization primitives. Common ones
are mutexes, semaphores, condition variables and spin locks. They are all variants of the same concept: they protect a piece of code by allowing only
Expand Down