第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

errno是線程安全的嗎?

errno是線程安全的嗎?

慕仙森 2019-10-06 13:23:21
在中errno.h,此變量被聲明為extern int errno;是我的問(wèn)題,errno在某些調(diào)用之后檢查值還是在多線程代碼中使用perror()是安全的。這是線程安全變量嗎?如果沒(méi)有,那還有什么選擇呢?我在x86體系結(jié)構(gòu)上將Linux與gcc一起使用。
查看完整描述

3 回答

?
開(kāi)心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超13個(gè)贊

Errno不再是一個(gè)簡(jiǎn)單的變量,它是幕后復(fù)雜的事情,特別是它具有線程安全性。


見(jiàn)$ man 3 errno:


ERRNO(3)                   Linux Programmer’s Manual                  ERRNO(3)


NAME

       errno - number of last error


SYNOPSIS

       #include <errno.h>


DESCRIPTION


      ...

       errno is defined by the ISO C standard to be  a  modifiable  lvalue  of

       type  int,  and  must not be explicitly declared; errno may be a macro.

       errno is thread-local; setting it in one thread  does  not  affect  its

       value in any other thread.

我們可以仔細(xì)檢查:


$ cat > test.c

#include <errno.h>

f() { g(errno); }

$ cc -E test.c | grep ^f

f() { g((*__errno_location ())); }


查看完整回答
反對(duì) 回復(fù) 2019-10-06
?
慕慕森

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊

在errno.h中,此變量聲明為extern int errno;


這是C標(biāo)準(zhǔn)所說(shuō)的:


宏errno不必是對(duì)象的標(biāo)識(shí)符。它可能會(huì)擴(kuò)展為由函數(shù)調(diào)用(例如*errno())產(chǎn)生的可修改的左值。


通常,errno是一個(gè)宏,它調(diào)用一個(gè)函數(shù),該函數(shù)返回當(dāng)前線程的錯(cuò)誤號(hào)的地址,然后將其取消引用。


這是我在Linux上的/usr/include/bits/errno.h:


/* Function to get address of global `errno' variable.  */

extern int *__errno_location (void) __THROW __attribute__ ((__const__));


#  if !defined _LIBC || defined _LIBC_REENTRANT

/* When using threads, errno is a per-thread value.  */

#   define errno (*__errno_location ())

#  endif

最后,它將生成這種代碼:


> cat essai.c

#include <errno.h>


int

main(void)

{

    errno = 0;


    return 0;

}

> gcc -c -Wall -Wextra -pedantic essai.c

> objdump -d -M intel essai.o


essai.o:     file format elf32-i386



Disassembly of section .text:


00000000 <main>:

   0: 55                    push   ebp

   1: 89 e5                 mov    ebp,esp

   3: 83 e4 f0              and    esp,0xfffffff0

   6: e8 fc ff ff ff        call   7 <main+0x7>  ; get address of errno in EAX

   b: c7 00 00 00 00 00     mov    DWORD PTR [eax],0x0  ; store 0 in errno

  11: b8 00 00 00 00        mov    eax,0x0

  16: 89 ec                 mov    esp,ebp

  18: 5d                    pop    ebp

  19: c3                    ret


查看完整回答
反對(duì) 回復(fù) 2019-10-06
  • 3 回答
  • 0 關(guān)注
  • 875 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)