吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C中的getchar_unlocked()

C中的getchar_unlocked()

来源:网络 | 更新时间:2022-01-08 20:12:32
getchar_unlocked()与 getchar() 类似,但它不是线程安全的。下面是一个示例代码。
// A simple C program to demonstrate
// working of getchar_unlocked()
#include <stdio.h>
int main()
{
// Syntax is same as getchar()
char c = getchar_unlocked();

printf("Entered character is %c", c);

return 0;
}
Input: g
Output: Entered character is g
以下是一些要点:
  1. 由于它不是线程安全的,因此避免了所有互斥开销,并且比 getchar() 更快。
  2. 对于具有“警告:大 I/O 数据,小心某些语言(尽管如果算法设计良好,大多数应该没问题)”的竞争性编程问题特别有用。
  3. 即使在多线程环境中使用 getchar_unlocked() 也没有问题,只要使用它的线程是访问文件对象的唯一线程
  4. 与 getchar() 的另一个区别是,它不是 C 标准库函数,而是 POSIX 函数。它可能不适用于基于 Windows 的编译器。
  5. 众所周知,通常 scanf() 比 cin 快,而 getchar() 通常比 scanf() 快。getchar_unlocked() 比 getchar() 快,因此速度最快。
  6. 类似地,getc_unlocked() putc_unlocked() 和 putchar_unlocked() 分别是 getc()、putc() 和 putchar() 的非线程安全版本。
// A simple C program to demonstrate
// working of putchar_unlocked()
#include <stdio.h>
int main()
{
// Syntax is same as getchar()
char c = getchar_unlocked();

putchar_unlocked(c);

return 0;
}
Input: g
Output: g
 

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com