简体   繁体   English

有多少种方法可以锁定C

[英]There are how many ways to lock in c

I am quite a newbie to c programming. 我是C编程的新手。 Until now i only found pthread_mutex_lock can make the code region run only by one thread. 到目前为止,我只发现pthread_mutex_lock可以使代码区域仅由一个线程运行。 Does there are any other ways to implement a lock? 还有其他实现锁的方法吗? Or every other way to do a lock is still use pthread_mutex_lock function? 还是其他所有方法仍然使用pthread_mutex_lock函数?

Threads were only introduced into the ISO C standard with C11, a rather recent edition to the standard so not necessarily widely supported yet. 线程仅通过C11引入ISO C标准中,C11是该标准的最新版本,因此不一定得到广泛支持。

You need to look into threads.h and the mtx_* functions for an understanding of that. 你需要寻找到threads.hmtx_*功能的,一个理解。

Before then, pthreads was probably your best bet with its wide implementation although, not being standard C (a) , its support wasn't mandated. 在此之前,尽管不是标准C (a) ,但没有强制要求支持pthreads ,但其广泛的实现可能是最好的选择。

For example, Windows has its own way of doing threading, using functions like CreateThread . 例如,Windows使用诸如CreateThread类的CreateThread拥有自己的线程处理方式。

However, there are various third-party products such as pthreads-win32 that aim to give pthreads support to Windows, to assist in porting of applications from POSIX-compliant operating systems. 但是,有许多第三方产品,例如pthreads-win32 ,旨在向Windows提供pthread支持,以帮助从兼容POSIX的操作系统移植应用程序。


(a) It is a POSIX standard (part of IEEE 1003.1) so that may be good enough for some people. (a) POSIX标准(IEEE 1003.1的一部分),因此对于某些人来说可能已经足够好了。

There is no way to lock in the c language. 无法锁定c语言。 Operating systems might provide support for locking (without regard for the language), and libraries such as pthreads can take advantage of operating system services, however this is beside the language. 操作系统可能会提供对锁定的支持(不考虑语言),而诸如pthreads库可以利用操作系统服务,但是这是该语言之外的。 (By contast, other languages have native locking built into them, such as through Java's synchronized keyword.) (与此同时,其他语言也内置了本机锁定,例如通过Java的synchronized关键字。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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