Critical section in linux

critical section for linux

In linux or a library for linux, is there an equivalent to critical section in win32? I am aware of Mutex, but it is not the same as critical section since a critical section uses user-mode spinlock and an event object internally, and it is must be faster than mutex.

Please be more precise about what you are looking for. As can be read in th wikipedia article about critical section, the application-level (user-space) critical section under linux uses mutex.

«it must be faster than a mutex» is not generally true, depends on how the mutex is implemented. Mutex implementations on Windows and Linux (pthreads) are potentially very different.

thank you. I thought the implementations of mutex in windows and linux are similar, because implementing it is widely known.

2 Answers 2

If you want something coming from kernel space, then you might want to look at semaphores ( sem_overview(7) ). You can built higher level constructs from a semaphore, like «event», «condition», «mutex» («critical sections»). There are the older and newer interfaces in C. Some higher level languages like Python and Perl also expose the interface.

The «Mutex» that you are likely talking about is the pthread’s mutex, which will be faster than anything in user space, especially one using a spinlock (which were designed for extremely low level OS level constructs). Some pthread’s implementations may use an OS level semaphore or may use other constructs.

Читайте также:  Ultraiso аналог в linux

Under Linux, the POSIX mutex is implemented by a featured kernel facility named futex(2) which is a part of NPTL. It means that your thread traps into kernel-space ONLY when it needs suspending, or just a simple user-space function.

Spin-lock is so simple that needn’t the kernel be involved at all, you can easily construct it just by something like __sync_bool_compare_and_swap(). Glibc implemented spin lock of cause — man pthread_spin_init — , but spin lock is not always «fast» as your imagination.

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Thompson’s Technological Insight

Thompson's Technological Insight

Hello Thompson,
I found this helpful as well. However, why did you choose PTHREAD_MUTEX_RECURSIVE_NP instead of PTHREAD_MUTEX_NORMAL?

It is just an example. You can use either of them depend on your use case

PTHREAD_MUTEX_NORMAL is usually the default and only allow thread to acquires onces

PTHREAD_MUTEX_RECURSIVE is a recursive mutex that allow thread to acquire many time. But, you need to release the lock the same amount of time before other can reuse it. Reply Delete

Читайте также:  Can you use linux on mac

Post a Comment

Image

MP4 File Format Part 1

  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps

.Net Framework — How to check the version installed

  • Get link
  • Facebook
  • Twitter
  • Pinterest
  • Email
  • Other Apps

Archive

  • April 2022 1
  • March 2022 1
  • February 2022 1
  • January 2022 1
  • December 2021 2
  • November 2021 1
  • October 2021 1
  • September 2021 1
  • August 2021 1
  • July 2021 1
  • June 2021 1
  • May 2021 2
  • April 2021 1
  • March 2021 1
  • January 2021 2
  • December 2020 1
  • October 2020 1
  • September 2020 1
  • August 2020 1
  • June 2020 1
  • May 2020 1
  • April 2020 1
  • March 2020 1
  • January 2020 2
  • October 2019 1
  • September 2019 1
  • August 2019 1
  • July 2019 1
  • June 2019 1
  • May 2019 1
  • April 2019 1
  • March 2019 1
  • February 2019 1
  • January 2019 1
  • December 2018 1
  • November 2018 1
  • October 2018 1
  • September 2018 1
  • August 2018 1
  • July 2018 1
  • June 2018 1
  • May 2018 1
  • April 2018 1
  • March 2018 1
  • February 2018 1
  • December 2017 1
  • October 2017 1
  • September 2017 1
  • August 2017 1
  • July 2017 1
  • May 2017 1
  • April 2017 1
  • March 2017 1
  • February 2017 1
  • January 2017 1
  • November 2016 1
  • October 2016 1
  • September 2016 2
  • August 2016 3
  • July 2016 2
  • May 2016 1
  • April 2016 1
  • March 2016 1
  • February 2016 1
  • January 2016 1
  • December 2015 1
  • November 2015 1
  • October 2015 1
  • September 2015 1
  • August 2015 2
  • July 2015 1
  • June 2015 1
  • May 2015 1
  • April 2015 1
  • March 2015 1
  • February 2015 1
  • December 2014 1
  • November 2014 1
  • October 2014 1
  • September 2014 1
  • August 2014 1
  • July 2014 1
  • June 2014 1
  • May 2014 2
  • April 2014 1
  • March 2014 1
  • February 2014 2
  • January 2014 2
  • December 2013 3
  • November 2013 3
  • October 2013 2
  • September 2013 2
  • August 2013 4
  • July 2013 5
  • June 2013 6
  • May 2013 4
  • April 2013 3
  • March 2013 2
  • February 2013 2
  • January 2013 4
  • December 2012 2
  • November 2012 1
  • October 2012 1
  • September 2012 2
  • August 2012 3
  • July 2012 3
  • June 2012 3
  • May 2012 7
  • April 2012 3
  • March 2012 5
  • February 2012 3
  • January 2012 4
  • December 2011 6
  • November 2011 2
  • October 2011 1
  • September 2011 12
  • August 2011 4
  • July 2011 5
  • June 2011 3
  • May 2011 6
  • April 2011 4
  • March 2011 10
  • February 2011 11
  • January 2011 4
  • November 2010 5
  • October 2010 16
  • September 2010 1
  • August 2010 5
  • July 2010 1
  • June 2010 6
  • May 2010 5
  • April 2010 11
  • March 2010 4
  • February 2010 1
  • January 2010 1
  • December 2009 1
  • November 2009 3
  • October 2009 3
  • September 2009 9
  • August 2009 6
  • July 2009 3
  • June 2009 7
  • May 2009 6
  • April 2009 9
  • March 2009 5
  • February 2009 16
Читайте также:  Dos commands and linux commands

Источник

Оцените статью
Adblock
detector