From 03851e8991184a2c44f561a202f1da6084c19d0d Mon Sep 17 00:00:00 2001 From: Adrien Date: Tue, 30 Jan 2024 22:08:09 +0100 Subject: [PATCH 1/9] comment on thread safety --- tutorials/2.6/graphics-draw-fr.php | 8 +++++++- tutorials/2.6/graphics-draw.php | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tutorials/2.6/graphics-draw-fr.php b/tutorials/2.6/graphics-draw-fr.php index 396dbc1c4..c579585d7 100644 --- a/tutorials/2.6/graphics-draw-fr.php +++ b/tutorials/2.6/graphics-draw-fr.php @@ -182,12 +182,18 @@ }

- 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.

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 tutoriel sur les fenêtres.

+

+ Bien que permette de dessiner dans un thread et de gérer ses évènements dans un autre, ses autres fonctions ne sont pas + pour autant thread-safe. + En particulier, vous devez arrêter le thread de dessin avant d'appeler la fonction close. +

- 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 : 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.

Remember to always create the window and handle its events in the main thread for maximum portability. This is explained in the window tutorial.

+

+ Even if allows you to draw in a thread and handle its events in another, it's other functions are not thread-safe. + In particular, you must stop the rendering thread before calling the function close. +

Date: Tue, 30 Jan 2024 23:11:50 +0100 Subject: [PATCH 2/9] reformulation --- tutorials/2.6/graphics-draw-fr.php | 6 +++--- tutorials/2.6/graphics-draw.php | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tutorials/2.6/graphics-draw-fr.php b/tutorials/2.6/graphics-draw-fr.php index c579585d7..3cec90d83 100644 --- a/tutorials/2.6/graphics-draw-fr.php +++ b/tutorials/2.6/graphics-draw-fr.php @@ -190,9 +190,9 @@ tutoriel sur les fenêtres.

- Bien que permette de dessiner dans un thread et de gérer ses évènements dans un autre, ses autres fonctions ne sont pas - pour autant thread-safe. - En particulier, vous devez arrêter le thread de dessin avant d'appeler la fonction close. + Vous pouvez dessiner et gérer les évènements d'une fenêtre dans deux threads différents car ces deux fonctionnalités sont complètement indépendantes. + La classe n'est autrement pas thread-safe. + En particulier, vous devez vous assurer que la fenêtre ne soit plus utilisée que dans un seul thread avant de la fermer.

window tutorial.

- Even if allows you to draw in a thread and handle its events in another, it's other functions are not thread-safe. - In particular, you must stop the rendering thread before calling the function close. + You can draw and handle the events of a window in two different threads because these two functionalities dont share anything. + The class is otherwise not thread-safe. + In particular, you must ensure the window is only used in one thread before closing it.

Date: Thu, 1 Feb 2024 21:00:34 +0100 Subject: [PATCH 3/9] comment on thread safety --- tutorials/2.6/system-thread-fr.php | 4 ++++ tutorials/2.6/system-thread.php | 3 +++ 2 files changed, 7 insertions(+) diff --git a/tutorials/2.6/system-thread-fr.php b/tutorials/2.6/system-thread-fr.php index e2b7e5e5c..d6b16cbd5 100644 --- a/tutorials/2.6/system-thread-fr.php +++ b/tutorials/2.6/system-thread-fr.php @@ -227,6 +227,10 @@ plusieurs threads en même temps. Et si l'opération en question n'est pas thread-safe, le résultat sera indéterminé (c'est-à-dire que cela pourrait planter ou corrompre des données).

+

+ Les instances des classes fournies par 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 ! +

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 spin locks. diff --git a/tutorials/2.6/system-thread.php b/tutorials/2.6/system-thread.php index 193b1aa4d..f0377b47a 100644 --- a/tutorials/2.6/system-thread.php +++ b/tutorials/2.6/system-thread.php @@ -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 thread-safe, it can lead to undefined behavior (ie. it might crash or corrupt data).

+

+ Instances of classes defined by SFML are usually not thread-safe. You must protect them if you use them in multiple threads at the same time ! +

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 From a31a1498c978afd631765b5e8e8553f3dbe6d4bf Mon Sep 17 00:00:00 2001 From: Adrien Date: Thu, 1 Feb 2024 21:28:07 +0100 Subject: [PATCH 4/9] faq --- faq.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/faq.php b/faq.php index 681298aa1..8d3d9d4cb 100644 --- a/faq.php +++ b/faq.php @@ -75,6 +75,7 @@

  • How do I use sf::Mutex?
  • Why can't I store my sf::Thread in an STL container?
  • Why doesn't sf::sleep sleep for the amount of time I want it to?
  • +
  • Is SFML thread-safe?
  • Programming in General

    @@ -588,6 +589,10 @@

    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.

    In the end, what this means is that sf::sleep() 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.

    +

    Is SFML thread-safe?

    +

    No, instances of SFML's classes are not thread-safe by themselves. If you need thread-safety you must protect them. SFML comes with sf::Mutex and sf::Lock for this purpose but you can use other tools you may have available such as C++11's std::mutex and std::lock_guard as you see fit.

    +

    For more information on how to protect you shared data, refer to the official documentation.

    +

    Programming in General

    What is RAII and why does it rock?

    From 55a0b6f2e32d0fb3404c7272db5f43d553b1d7ba Mon Sep 17 00:00:00 2001 From: Adrien Date: Thu, 1 Feb 2024 23:45:14 +0100 Subject: [PATCH 5/9] comment --- tutorials/2.6/graphics-draw-fr.php | 10 ++++++---- tutorials/2.6/graphics-draw.php | 12 +++++++----- tutorials/2.6/system-thread-fr.php | 3 +-- tutorials/2.6/system-thread.php | 2 +- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/tutorials/2.6/graphics-draw-fr.php b/tutorials/2.6/graphics-draw-fr.php index 3cec90d83..84a21f1be 100644 --- a/tutorials/2.6/graphics-draw-fr.php +++ b/tutorials/2.6/graphics-draw-fr.php @@ -187,12 +187,14 @@

    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 - tutoriel sur les fenêtres. + tutoriel sur les fenêtres.

    - Vous pouvez dessiner et gérer les évènements d'une fenêtre dans deux threads différents car ces deux fonctionnalités sont complètement indépendantes. - La classe n'est autrement pas thread-safe. - En particulier, vous devez vous assurer que la fenêtre ne soit plus utilisée que dans un seul thread avant de la fermer. + 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 + protéger les données partagées.
    + 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.

    - Note : you don't need to activate the window in the rendering thread, SFML does it automatically for you whenever it needs + 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.

    Remember to always create the window and handle its events in the main thread for maximum portability. This is explained in the - window tutorial. + window tutorial.

    - You can draw and handle the events of a window in two different threads because these two functionalities dont share anything. - The class is otherwise not thread-safe. - In particular, you must ensure the window is only used in one thread before closing it. + The example show 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 + protect the shared data yourself.
    + Note that you can draw and handle the events of a window in two different threads without issues because these + two functionalities are independent.

    - Les instances des classes fournies par 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 ! + 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 !

    Il existe plusieurs outils de programmation pour vous aider à protéger les variables partagées et rendre votre code thread-safe, ils sont diff --git a/tutorials/2.6/system-thread.php b/tutorials/2.6/system-thread.php index f0377b47a..bff947a0c 100644 --- a/tutorials/2.6/system-thread.php +++ b/tutorials/2.6/system-thread.php @@ -223,7 +223,7 @@ classes are a good solution. If the operation is not thread-safe, it can lead to undefined behavior (ie. it might crash or corrupt data).

    - Instances of classes defined by SFML are usually not thread-safe. You must protect them if you use them in multiple threads at the same time ! + SFML objects are usually not thread-safe, you must protect them to use them in multiple threads at the same time !

    Several programming tools exist to help you protect shared data and make your code thread-safe, these are called synchronization primitives. Common ones From 498dc56ab4bf8744c9031732dc57d6468a3ac7aa Mon Sep 17 00:00:00 2001 From: frayien Date: Fri, 2 Feb 2024 19:57:31 +0100 Subject: [PATCH 6/9] Update tutorials/2.6/system-thread.php Co-authored-by: kimci86 --- tutorials/2.6/system-thread.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/2.6/system-thread.php b/tutorials/2.6/system-thread.php index bff947a0c..ed12e7aff 100644 --- a/tutorials/2.6/system-thread.php +++ b/tutorials/2.6/system-thread.php @@ -223,7 +223,7 @@ classes are a good solution. If the operation is not thread-safe, it can lead to undefined behavior (ie. it might crash or corrupt data).

    - SFML objects are usually not thread-safe, you must protect them to use them in multiple threads at the same time ! + SFML objects are usually not thread-safe. You must protect them to use them in multiple threads at the same time!

    Several programming tools exist to help you protect shared data and make your code thread-safe, these are called synchronization primitives. Common ones From 7b747d22ff495b3c86310a017536b304afe66388 Mon Sep 17 00:00:00 2001 From: frayien Date: Fri, 2 Feb 2024 19:59:19 +0100 Subject: [PATCH 7/9] Update tutorials/2.6/system-thread-fr.php Co-authored-by: kimci86 --- tutorials/2.6/system-thread-fr.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/2.6/system-thread-fr.php b/tutorials/2.6/system-thread-fr.php index a18bdff87..c3dfb1dcd 100644 --- a/tutorials/2.6/system-thread-fr.php +++ b/tutorials/2.6/system-thread-fr.php @@ -228,7 +228,7 @@ cela pourrait planter ou corrompre des données).

    - 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 ! + 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 !

    Il existe plusieurs outils de programmation pour vous aider à protéger les variables partagées et rendre votre code thread-safe, ils sont From bd1653d1c3a382f1dd5eef58cd08c319c4d76df8 Mon Sep 17 00:00:00 2001 From: frayien Date: Fri, 2 Feb 2024 19:59:57 +0100 Subject: [PATCH 8/9] Update tutorials/2.6/graphics-draw.php Co-authored-by: kimci86 --- tutorials/2.6/graphics-draw.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tutorials/2.6/graphics-draw.php b/tutorials/2.6/graphics-draw.php index 2da431dbc..59754bfc8 100644 --- a/tutorials/2.6/graphics-draw.php +++ b/tutorials/2.6/graphics-draw.php @@ -185,7 +185,7 @@ function. window tutorial.

    - The example show here is not completely thread-safe because a thread could close the window while another still uses it. + 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 protect the shared data yourself.
    Note that you can draw and handle the events of a window in two different threads without issues because these From f7546e457a8f179cae144c61d179521068231075 Mon Sep 17 00:00:00 2001 From: frayien Date: Fri, 2 Feb 2024 19:48:33 +0000 Subject: [PATCH 9/9] instance of class = object --- faq.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/faq.php b/faq.php index 8d3d9d4cb..a8374a3df 100644 --- a/faq.php +++ b/faq.php @@ -590,7 +590,7 @@

    In the end, what this means is that sf::sleep() 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.

    Is SFML thread-safe?

    -

    No, instances of SFML's classes are not thread-safe by themselves. If you need thread-safety you must protect them. SFML comes with sf::Mutex and sf::Lock for this purpose but you can use other tools you may have available such as C++11's std::mutex and std::lock_guard as you see fit.

    +

    No, SFML objects are not thread-safe by themselves. If you need thread-safety you must protect them. SFML comes with sf::Mutex and sf::Lock for this purpose but you can use other tools you may have available such as C++11's std::mutex and std::lock_guard as you see fit.

    For more information on how to protect you shared data, refer to the official documentation.

    Programming in General