fwrite fwrite_unlocked

less than 1 minute read

背景

fwrite 和 fwrite_unlocked是一对,其中fwrite_unlocked是fwrite的线程不安全版本,因为不加锁。

参考

1. man fwrite

NAME
fread, fwrite - binary stream input/output
……

RETURN VALUE
fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count
(or zero).

2. man unlocked_stdio

   size_t fwrite_unlocked(const void *ptr, size_t size, size_t n,  
                         FILE *stream);   ......  
   getc_unlocked(), getchar_unlocked(), putc_unlocked(), putchar_unlocked(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE || _BSD_SOURCE || _SVID_SOURCE  
  
   clearerr_unlocked(), feof_unlocked(), ferror_unlocked(), fileno_unlocked(), fflush_unlocked(), fgetc_unlocked(), fputc_unlocked(), fread_unlocked(), fwrite_unlocked(): _BSD_SOURCE || _SVID_SOURCE  
  
   fgets_unlocked(), fputs_unlocked(), getwc_unlocked(), getwchar_unlocked(), fgetwc_unlocked(), fputwc_unlocked(), putwchar_unlocked(), fgetws_unlocked(), fputws_unlocked(): _GNU_SOURCE  

DESCRIPTION
Each of these functions has the same behavior as its counterpart without the “_unlocked” suffix, except that they do not use locking (they do not set locks themselves, and do not test for the presence of locks set by others) and hence are thread-unsafe. See flockfile(3).

Flag Counter

digoal’s 大量PostgreSQL文章入口