简体   繁体   中英

How to use spinlock in linux?

I am a computer science student, I have one assignment asking me to use spinlock to lock a thread and unlock it once its critical section is completed. The difficulty is I googled many times, but I did not find any useful information. For example, I tried to include header like #include <linux/spinlock.h> , but when I use gcc to compile this c program, it says cannot find such header. So, what header I should include and what functions I need to call in order to use spinlock?

linux/spinlock.h is a part of the Linux kernel headers. It isn't a header for Linux program development.

Spinlocks are only suitable when the competing threads are running on different cores. Outside of kernels and certain specialized applications that control exactly which threads are running on which core, what you need is a proper mutex, not a spinlock. Look up the pthread part of the standard Library, specifically the pthread_mutex_xxx functions declared in pthread.h .

If the assignment specifically asks for a spin lock then the goal may be for you to implement a spinlock as a learning exercise. How to do that depends on what primitives you have: the primitives to implement a spinlock depend on the CPU and how to access them depends on the programming language (and for C on the compiler).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM