What is getchar_unlocked()? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is getchar_unlocked()?

I recently made a profile on CodeChef and found out that many people use getchar_unlocked function for inputs there during competitions as it is faster in some way. It was described as a function for fast I/O... Can anyone explain its functioning to me in detail and how is it faster than cin?

5th Jun 2017, 9:42 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
15 Answers
+ 3
lets divide this question into parts so that u understand every bit of it: WHAT IS getchar_unlocked() AND why it is used for? In a POSIX environment, standard input/output functions like getchar() use mutexes to prevent concurrent access to the input buffer by multiple threads, which makes them thread safe (synchronized). An unlocked version of such functions means they do not use mutexes and do not check whether a mutex lock has been set by another thread on the input buffer, Now few terms that we have encountered above that needs to be clear: i)thread ii)POSIX WHAT ARE THEY: THREAD: I Start with an example .suppose ur college has planned to organize an annual fest . To make the fest a great success it breaks the work into different pieces and assign different piece of work among different people .They all complete their own piece of work and the fest becames a great success. Similarly an big program can be divided into different parts called threads and each thread can work independent of the other .when there are more than one thread in a program it is called a multithreaded program. Now what getchar does is that it prevents concurrrent access of the input buffer by different threads.That is two different threads are not allowed to access the input buffer at the same time.This is needed to ensure smooth functioning of the program.what getchar_unlocked() does that it does not check if multiple thread are trying to access the input buffer simultaneously or not . so it boosts time .
5th Jun 2017, 4:46 PM
NIKHIL MURARKA
+ 4
by setting the ios flag sync_with_stdio to false. basically iostream needs to sync with stdio.h to do some magic. But if you are not going to use any c style headers that deal with streams you can turn this sync off. I think thats the reasoning behind it
5th Jun 2017, 11:18 AM
jay
jay - avatar
+ 4
someone did tests on stackoverflow link. the second explains the theory i believe
5th Jun 2017, 11:33 AM
jay
jay - avatar
+ 3
@jay Thanks for your efforts...
5th Jun 2017, 11:11 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Apparently cin can be made much.much faster
5th Jun 2017, 11:14 AM
jay
jay - avatar
+ 3
How?
5th Jun 2017, 11:16 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
Ill have to look more into io for this...
5th Jun 2017, 11:20 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 3
my answer is big therefore it is not geeting uploaded therefore i am breaking my answer into two parts
5th Jun 2017, 4:42 PM
NIKHIL MURARKA
+ 3
POSIX:C++ does not contain any built-in support for applications. Instead, it relies entirely upon the operating system to provide this feature. if we assume that we are working on Linux OS and we are going to write multi-threaded C++ program using POSIX.it is simply a way to declare multithreaded function. WHY DO WE NEED /USE getchar_unlocked()? i)it increases performance since there is an absence of overhead caused by using mutual exclusion. That is why getchar_unlocked() offers faster performance than getchar(). It's perfectly safe to use unlocked functions in a single-threaded application where performance is an important factor, otherwise it is recommended not to use getchar_unlocked(), especially in a multithreaded application.
5th Jun 2017, 4:46 PM
NIKHIL MURARKA
+ 2
@NIKHIL Thank You Very Much! Your answer is so explanatory!
6th Jun 2017, 3:25 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 2
@NIKHIL Can you also explain how to use it in a simple code snippet? Sorry to disturb 😅😶 Thank You in Advance...
6th Jun 2017, 3:27 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
@kinshuk SYNOPSIS #include <stdio.h> int getchar_unlocked(void); for more info check this link out: http://www.mkssoftware.com/docs/man3/getchar_unlocked.3.asp
6th Jun 2017, 8:21 AM
NIKHIL MURARKA
+ 1
@NIKHIL Thank You very much!
6th Jun 2017, 8:29 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar