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

Author Topic: trylock() patch  (Read 1979 times)

0 Members and 1 Guest are viewing this topic.

ccfreak2k

  • Newbie
  • *
  • Posts: 2
    • View Profile
trylock() patch
« on: January 05, 2014, 11:49:24 pm »
Since SML's Mutex class was lacking a trylock() method, I've decided to add one since I didn't want to drag in an entire new library just for that. Haven't been able to test the patch yet since I'm trying to build SFML still, but I think it should work. Still, caveat emptor. Would appreciate someone on Linux checking it since I'm on Windows.

Worth noting is that the return value is based on TryEnterCriticalSection(), so "true" means the mutex was locked and "false" means it was not, even on Unix platforms.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10827
    • View Profile
    • development blog
    • Email
Re: trylock() patch
« Reply #1 on: January 06, 2014, 12:28:53 am »
SFML does not try to be an concurrency library, thus only the features that are needed by SFML itself make it into SFML. With C++11 we already get quite a few useful features (including try_lock) and it's essentially advised to use those over SFML's implementations if possible.

Of course you're free to code and make public whatever you want with SFML, but don't expect to see this feature in SFML. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ccfreak2k

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: trylock() patch
« Reply #2 on: January 06, 2014, 02:36:27 am »
  • If SFML "does not try to be an concurrency library," why does it have a Mutex class in the first place? The existing one is a wrapper for the system libraries anyway, and both Windows and Linux (pthread users, anyway) have both lock and trylock, but only the former is exposed by SFML.
  • Not everyone is using C++11, though the number of projects using it grows every day.
  • I have no expectation for it to be officially included; I'm simply offering it as a patch for anyone that desires this functionality.

I only made this to add what I thought was lacking in the library. Where there is lock() there should be trylock() as well...and now there is.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10827
    • View Profile
    • development blog
    • Email
AW: trylock() patch
« Reply #3 on: January 06, 2014, 08:03:07 am »
The mutex exists, because it's used by the sf::Music and sf: Sound class, since they spawn a new thread for playing the audio. That's also where sf::Thread gets used, as well as for networking. ;)

But as I said, you're free to do whatever you want. :)

Personally I don't see a reason to not use C++11 anymore, although I understand when companies force an old compiler up on the employees or one owns an old VS version. If that's the case, I'd start now to push for a newet version.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/