×

# [closed] Difference between getchar() & getchar_unlocked()

 3 getchar_unlocked() is usually used to get faster input. But I have not understood the difference between the getchar_unlocked() and getchar(). Also, why is getchar_unlocked() faster and what does the term "thread safe" mean? asked 21 Dec '13, 14:08 226●2●7●12 accept rate: 20%

### The question has been closed for the following reason "The question is answered, right answer was accepted" by sananth12 25 Jan '14, 21:54

 10 @sananth12 getchar___unlocked() is the thread unsafe version of getchar(). The reason that getchar_unlocked() seems faster is that it doesn't check for any locks on the input stream from where it is supposed to fetch a character. Speed Comparison : getchar_unlocked > getchar > scanf > cin  Unless speed factor is too much necessary, try to avoid getchar_unlocked. Standard I/O functions need to lock mutexes and prevent simultaneous access to input buffer, that is why is they are thread safe. Unlocked versions of these functions do not set locks themselves, and do not test for the presence of locks set by others, thus decreasing overhead of mutual exclusion. These functions may be used to avoid the overhead of locking the stream for each character, and to avoid input being dispersed among multiple threads reading from the same stream. Thanks & Regards CrucifiX answered 21 Dec '13, 14:23 1★crucifix 721●3●8●10 accept rate: 18%
 6 getchar_unlocked() is thread unsafe version of getchar(). There is no input stream lock check in getchar_unlocked() which makes it unsafe. A piece of code is thread safe when more than one thread can execute the same code without causing synchronization problems. http://www.thegeekstuff.com/2012/07/c-thread-safe-and-reentrant/ http://techpubs.sgi.com/library/tpl/cgi-bin/getdoc.cgi?coll=0650&db=man&fname=/usr/share/catman/p_man/cat3s/getchar_unlocked.z http://stackoverflow.com/questions/9052757/getchar-unlocked-vs-scanf-vs-cin answered 21 Dec '13, 14:26 3★laxman94 1.2k●3●15●15 accept rate: 6%
 0 From where to study everything about getchar() and getchar_unlocked() ? I am really getting confused.. answered 21 Dec '13, 19:01 3★damn_me 2.6k●2●13●36 accept rate: 24% 1 I found this about getchar() quite detailed. (21 Dec '13, 21:31)

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×28
×19

question asked: 21 Dec '13, 14:08

question was seen: 9,208 times

last updated: 25 Jan '14, 21:54