Frequently asked thread questions in C Linux

Q)When should thread be used?
Threading could be used in operations required like:
i)Asynchronous operations
ii)Operations that can be parallelized
iii)Continual running background operations
Q)What is Asynchronous operations?
A synchronous operation blocks a process till the operation completes. 
An asynchronous operation is non-blocking and only initiates the operation. The caller could discover completion by some other mechanism.
Q)What are the difference between a mutex and a semaphore?
i)Mutex as the name suggests is used for mutually exclusive usage of shared resource by any one thread at a time. ii)The thread which has locked the mutex that only can release the lock. While in case of semaphore this not so.
i) In case of semaphore specified numbers of threads can use the shared resource simultaneously. ii)Also It is not necessary that the locking thread only release the lock. It can be released by any thread.
Q) What is the difference between a binary semaphore and a mutex.?
In case of a mutex the thread which has locked the mutex that only can release the lock. While in case of semaphore this is not so. It is not necessary that the locking thread only release the lock. It can be released by any thread.
Q) What is POSIX?
Portable Operating System Interface. Posix is standarad based thread API's for C and C++. It provides APIs for thread creation,termination,synchronization(join,blocking), data management,scheduling and process interaction.
Q) What is thread and why it is used?
Threads are function under execution. Threads are used to make software more responsive and faster by introducing parallelism through multiple process. Thread shows better result in multi core system as single processor machines run as single threaded application and scheduling between threads are so fast that it appears fast.
Q) Why threads are fast?
Threads are faster to create and terminate as threads share data segment of the process and they run on the same address space of the process.
Unlike threads process creation(forking) involves creation of different address spaces different apllications.
Two threads of same process share:
Data segment(global and static variable as these are stored in data segment).
Address space of the process.
Open file's file descriptor.
Current Directory
User and Group Id
Signal and signal handlers
Threads have unique or their own thread Ids:
Thread Id.
Stack, local variables and register
Program counter and return values like errno
priority
signal mask
Q) What is the syntax of a thread:
int pthread_create(pthread_t* thread, //pointer to the thread so call should pass address of the thread
                       const pthread_attr_t * attr,// PTHREAD_CREATE_JOINABLE is default also PTHREAD_CREATE_DETACHED
                       void* (*start_routine)(void *), //pointer to the function to be threaded.Function has a single argument:pointer to void
                       void* arg);//pointer to argument of function. To pass multiple arguments, send a pointer to a structure.

Q)What is the return type of thread_crate api?
pthread_create return the thread id on success and 0 on error while creating thread.
Q) What is the default attribute to the thread?
NULL ie default attribut which is joinable PTHREAD_CREATE_JOINABLE. other attribute can be set is  PTHREAD_CREATE_DETACHED.
Q) What is the last argument of create_thread attribute?
Last argument in thread is pointer to the argument of the function.
Q) How to pass multiple arguments to thread?
Pointer to structure can be used to pass multiple arguments to a thread in pthread_create api.
Q) What api is used to get thread id?
pthread_self();
Q) What does int pthread_join(pthread_t th, void **thread_return); does?
pthread_join makes the calling thread to waits till specified thread gets terminated or completed.
Q) What is the use of void pthread_exit(void *retval);?
pthread_exit terminates/kills the calling thread.
Q)How to check pthread_exit has killed/terminated the calling thread or not?
take the return value of api like int=pthread_exit(); it works on C and not in C++ as void return type of pthread exit.
Q)How to check pthread_create is successfull or not?
take the return value of api like int=pthread_create(); if 0 then failure else thread_id is returned. instead of int use unsigned long int.
Q) How Thread synchronization is achieved?
Using Mutex,condtion variables and joins.
Mutex: are used for mutual exclusive usage of global variables by any single thread at a time.
Conditional variables: are variable of type pthread_cond_t used with mutex to achieve atomic use of global variable and to avoid race condition
join: Join suspends the calling thread till specified thread completes its task. Join is used when it wants to wait for thread/threads it has created to get terminated/completed.
Q) What is race condition?
Race condition occurs when mutiple threads modifies the global variable but the result of the computation depends on the order of in which these threads perform on the shared global variable. Mutex are used to protect the critical section of a thread so that global variable can be used in protected manner by only one thread at a time.
//Without mutex
int counter=0;
void function1()
{
   counter++ //thread1 and thread2 both can modify and inconsistent result when any of the thread stores the modified value to their local register.
}
//With mutex
int counter=0;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;;
void function1()
{
pthread_mutex_lock(&mutex);
   counter++ //thread which has mutex that only can modify counter so blocking. so use condition variable.
pthread_mutex_unlock(&mutex);
}
Q) What is Condition variable and When it should be used?
Condition variable is a variable of type pthread_cond_t which is used with mutex for thread synchronization/serialization.
Condition variable allows a thread to suspend or release the processor till a condition is true. Condition variable must be used with mutex to avoid deadlock crated by thread by locking  it when other thread may wait for global variable to get ulocked.Conditional variable is used for signaling like hey I am done with my task with shared resource you may use it now.
Condition variable uses signal and wait mechanism to avoid deadlock situation.
Creating/Destroying:
   pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
   pthread_cond_init
   pthread_cond_destroy
Waiting on condition:
        pthread_cond_wait - unlocks the mutex and waits for the condition variable cond to be signaled.
        pthread_cond_timedwait - place limit on how long it will block.
Waking thread based on condition:
        pthread_cond_signal - restarts one of the threads that are waiting on the condition variable cond.
        pthread_cond_broadcast - wake up all threads blocked by the specified condition variable.
// Pthread example code in C++ with and without synchronization
//Thread example in C++. One thread should prints odd numbers and the other thread should print the even numbers. Example without synchronization.
#include<iostream>
using namespace std;
int count=0; //global variable accessed by two threads
int maxx=10;
void* even(void *v)
{
cout<<"even thread"<<endl;
while(count<maxx)
{
if((count%2)==0)
{
cout<<count++<<" ";
//count++;
}
}
pthread_exit(0);//pthread_exit is called from the thread itself to terminate its execution (and return a result) early.
}
void* odd(void *o)
{
cout<<"odd thread"<<endl;
while(count<maxx)
{
if((count%2)==1)
{
cout<<count++<<" ";
//count++;// then proper order :)
}
}
pthread_exit(0);
}

int main()
{
pthread_t t1;
pthread_t t2;

pthread_create(&t1,NULL,&even,NULL);
pthread_create(&t2,NULL,&odd,NULL);
pthread_join(t1,0);
pthread_join(t2,0);
//pthread_join is called from another thread to wait for a thread to terminate and obtain its return value
cout<<endl;
return 0;
}
//OUTPUT
~$ g++ test1.cpp -lpthread
~$ ./a.out
even thread
0 odd thread
1 3 2 5 4 76  89
//Improved Example synchronized using mutex and conditional variable.
#include<iostream>
using namespace std;
int count=0;
int maxx=10;
pthread_mutex_t mutex;
pthread_cond_t cond;
void* even(void *v)
{
    //cout<<"even thread"<<endl;
    while(count<maxx)
    {
        pthread_mutex_lock(&mutex);//lock before checking
        while((count%2)==0)
        {
            pthread_cond_wait(&cond,&mutex);
            //it releases the mutex and it waits till condition cond is signaled as complete and mutex is available.
        }
        cout<<"even "<<count++<<endl;
        pthread_mutex_unlock(&mutex);//release
        pthread_cond_signal(&cond);// restarts one of the threads that are waiting on the condition variable cond.
    }
    pthread_exit(0);//pthread_exit is called from the thread itself to terminate its execution (and return a result) early.
}
void* odd(void *o)
{
    while(count<maxx)
    {

        pthread_mutex_lock(&mutex);//lock before checking
        while((count%2)==1)
        {
            pthread_cond_wait(&cond,&mutex);//wait until count becomes odd
        }
        cout<<"odd "<<count++<<endl;
        pthread_mutex_unlock(&mutex);//release
        pthread_cond_signal(&cond);//signal to another thread
    }
    pthread_exit(0);
}
int main()
{
    pthread_t t1;
    pthread_t t2;

    pthread_mutex_init(&mutex, 0);
    pthread_cond_init(&cond, 0);

    pthread_create(&t1,NULL,&even,NULL);
    pthread_create(&t2,NULL,&odd,NULL);
    pthread_join(t1,0);
    pthread_join(t2,0);

    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);

    //pthread_join is called from another thread to wait for a thread to terminate and obtain its return value
    cout<<endl;
    return 0;
}
/*
odd 0
even1
odd 2
even3
odd 4
even5
odd 6
even7
odd 9
even10
*/
http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
http://www.bogotobogo.com/cplusplus/quiz_multithreading.php
https://linuxtrainers.wordpress.com/2014/12/22/importance-of-pthread-condition-variables/

Q) What are the common pitfalls in threading?
race condition, deadlock, no thread safe code.

Q) Race condition: As the threads are scheduled by OS so order is not fixed so unexpected result of computation on global variable by multiple threads. So mutex, join and condition variable must be used to achieve the proper order of function calling.
Q) Dead lock: Deadlock occurs when one thread lock and mutex and not unlock it. Deadlock may also occur when a thread lock a mutex and other threads trying lock mutex fails eventually thread will goes into locking mutex for ever so proper order of mutex lock and join must be used.
Q) Thread safety: A code is called thread safe when either it does not have global and static variable if there then mutex and join must be used.

Q) What is race condition?
Race condition is a bug in a multithreaded application. Race condition occurs when two or more threads access the same memory location like global variables simueltaneously. Suppose one global variable x is there which is modified by two threads t1 and t2 simueltaneously. Thread t1 modifies the global and saves in register variable again and again Similarly thread t2 modifies the global variable and stores the modified variable in local register vairble. When control goes out of threads the golbal is variable's value becomes unpredictable. So to protect global variable/ resource or to avoid race conditions mutex or locks are used.

Q) What is mutex?
Mutex or mutual exclusion is used for thread synchronization. Along with condtion variable mutex_wait and mutex_signal are used for pprotected access of global variable. So,mutex are locking mechanism which is used for thread synchronization to avoid race condition or to avoid global variable from simueltaneous access by two or more threads.
http://thispointer.com/c11-multithreading-part-5-using-mutex-to-fix-race-conditions/

Q) What is semaphore and what is the difference between mutex and semaphor?
Semaphore is a positive counter that can be either incremented or decremented. Semaphore is used for thread syncronization using sem_wait and sem_post.Semaphore is used when there is limited resource and need some mechanism to controll its uses. Suppose there are five chairs inside a meeting room and 10 peoples inside the room. So who will seat can be conrolled using 5 tokens. A person which has a token can seat into a chair. When 5 persons seating in chair counter is set to 0. When a person releases a chair counter is increased by one. Now one token is available so one person can seat again in chair and token count is decremented and made to 0.
So, what is the difference between mutex and semaphore?
Mutex follow ownership. the thread which locks the mutext, that thread only can release the lock.
In semaphore ownership of lock is not there. Any thread can release the lock. In above example any chair can be vaccated.

Q) What is condition variable and when it is necessary?
Condition variables are like event used between two or more threads for signals. One or more threads can wait on it to get signaled, while another thread can signal it. Mutex is required along with conditional variables.
It is mandatory to make the threads non blocking.

So, How things actually work with condition variable?
Thread 1 acquires the mutex and check if required condition is met or not.
If not then it releases the lock and waits for Condition Variable to get signaled ( thread gets blocked). Condition Variable’s wait() function provides both these operations in atomic manner.
Another Thread i.e. like Thread 2 signals the Condition Variable when condition is met
Once Conditional Variable get signaled the the Thread 1 which was waiting for it resumes. It then acquires the mutex lock again and checks if the condition associated with Condition Variable is actually met or if it is superiors call. If more than one thread was waiting then notify_one will unblock only one thread.
If it was a superiors call then it again calls the wait() function.


For example two threads has to modify a global variable count to print odd and even numbers respectively.
Thread one can lock the variable count does the operations to print odd and signals to thread two, while thread two waits till the variable become even and get signaled by thread one.
Thread one signals when value of count becomes even and then thread two prints the even numbers.
Example 2:
Suppose an application has to perform 3 tasks:
1: perform handshake to server.
2: wait to read data from a xml.
3: process the data.
Multithreading approach of above application would be like below:
Task1 is not dependent on other task.But,task2 and task3 are dependent to each other.
So, Task1 and Task2 can run paralelly to improve the performance.
So One thread can do
1: perform handshake to server.
2: wait to read data from a xml.
3: process the data.
Thread 2 can d:
2: wait to read from a xml
3:Notify other thread when data is available.

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

int MAX = 10;
int count = 0;

pthread_mutex_t mutex;
pthread_cond_t cond;

void *even(void *arg)
{
    while(count < MAX) {
        pthread_mutex_lock(&mutex;);
        while(count % 2 != 0) { //if odd number then enter into wait(lock also releasaed by mutex) else print the even number, unlock and                     //signal the other thread
            pthread_cond_wait(&cond;, &mutex;);
        }
        printf("%d ", count++);
        pthread_mutex_unlock(&mutex;);
        pthread_cond_signal(&cond;);
    }
    pthread_exit(0);
}

void *odd(void *arg)
{
    while(count < MAX) {
        pthread_mutex_lock(&mutex;);
        while(count % 2 != 1) {
            pthread_cond_wait(&cond;, &mutex;);
        }
        printf("%d ", count++);
        pthread_mutex_unlock(&mutex;);
        pthread_cond_signal(&cond;);
    }
    pthread_exit(0);
}

int main()
{
    pthread_t t1;
    pthread_t t2;

    pthread_mutex_init(&mutex;, 0);
    pthread_cond_init(&cond;, 0);

    pthread_create(&t1;, 0, &even;, NULL);
    pthread_create(&t2;, 0, &odd;, NULL);

    pthread_join(t1, 0);
    pthread_join(t2, 0);

    pthread_mutex_destroy(&mutex;);
    pthread_cond_destroy(&cond;);

    return  0;
}
The output looks like this:

0 1 2 3 4 5 6 7 8 9 10
http://www.bogotobogo.com/cplusplus/quiz_multithreading.php

// for even() thread function
pthread_mutex_lock(&mutex;);       (lock before checking)
check
   wait until count becomes even  (pthread_cond_wait(&cond;, &mutex;)
                                   - wait for signal from another thread)
do something                      (print and count++)

pthread_mutex_unlock(&mutex;);     (release)
pthread_cond_signal(&cond;);       (signal to another thread)

Q) Can two threads share the mutex?
Yes.
Q) Can two threads share the critical section?
No.
Q) What is critical section?
Critical section is section of code that modifies the global variable. Critical section code of any thread shoud be executed atomically only by one thread at a time else race condition occurs and global variable shows inconsistant value as threads modify and save the variables in their local registers.
Q) What is race condition?
A Condition when two or more thread tries to access a global variable simuetaneously. mutex or semaphore are used for protected access of global variable and avoid race condition.
Q) What is context switching?
Context switching means cpu swithcing between different processes or threads. In a single CPU system all processes or threads gets cpu time in round robin fashion. In multi core system if n cores then n processes can run independently.

CMakeLists.txt and cmake interview questions

STLSet all operation example code in C++ Linux

Set is an associated template container. Only values are stored in set.Once value inserted can not be modified.If want to modify then first erase then modify. In map using at() or [] we can directly insert new value. Values are stored in sorted ascending order. Effect of inserting a duplicate value does nothing.
#include<iostream>
using namespace std;
#include<set>
#include<algorithm>
int main()
{
cout<<"Creating an empty set:"<<endl;
set<int> set;
cout<<"Inserting into a set"<<endl;
set.insert(11);
set.insert(33);
set.insert(22);
set.insert(55);
set.insert(44);//does nothing
set.insert(44);//does nothing
set.insert(44);//does nothing

cout<<"iTraversing through into a set"<<endl;
std::set<int>::iterator itr=set.begin();
for(itr;itr!=set.end();itr++)
cout<<*itr<<" ";

cout<<"Searching in a set"<<endl;
itr=set.begin();
itr=set.find(55);
if(itr!=set.end())
cout<<"55 found"<<endl;
else
cout<<"55 not found"<<endl;

cout<<"Traversing"<<endl;
itr=set.begin();
for(itr;itr!=set.end();itr++)
cout<<*itr<<" ";


cout<<"finding and deleting from a set"<<endl;
itr=set.begin();
itr=set.find(22);
if(itr!=set.end())
{
cout<<"22 found"<<endl;
set.erase(22);
//itr=set.erase(22);//change to other itr2
}
else
{
cout<<"22 not found"<<endl;
}

set.erase(22);//deleting again no effect
cout<<"Traversing"<<endl;
itr=set.begin();
for(itr;itr!=set.end();itr++)
cout<<*itr<<" ";


cout<<"Deleting a range"<<endl;
std::set<int>::iterator itr2=set.begin();
std::set<int>::iterator itr3=set.end();
set.erase(itr2,itr3);

cout<<"Traversing"<<endl;
itr=set.begin();
for(itr;itr!=set.end();itr++)
cout<<*itr<<" ";

cout<<""<<endl;
return 0;
}
/*
Creating an empty set:
Inserting into a set
iTraversing through into a set
11 22 33 44 55 Searching in a set
55 found
Traversing
11 22 33 44 55 finding and deleting from a set
22 found
Traversing
11 33 44 55 Deleting a range
Traversing

*/

C++ list example. All operations.

#include<iostream>
using namespace std;
#include<list>
#include<algorithm>

int main()
{
//creating an empty list
list<int> l;
//Way:1 to insert
l.push_back(10);
l.push_back(100);
l.push_back(20);
//Way:2 to insert

list<int>::iterator itr=l.begin();
while(itr!=l.end())
{
cout<<*itr<<"\t";
itr++;
}

cout<<""<<endl;
//Way:2 creating a list with fixed size 5
list<string> ls2(5);
list<string>::iterator itr2=ls2.begin();
while(itr2!=ls2.end())
{
cout<<*itr2<<"\t";
itr2++;
}

cout<<""<<endl;
//Way:2 creating a list with fixed size 5 with initial value "Mr"
list<string> ls3(5,"Mr");
list<string>::iterator itr3=ls3.begin();
while(itr3!=ls3.end())
{
cout<<*itr3<<"\t";
itr3++;
}
cout<<""<<endl;

ls3.push_back("Mr");
ls3.push_back("Mr");
ls3.push_back("Mrs");
ls3.push_back("Miss");
ls3.push_back("Mrs");

cout<<"Way 0: to remove by value"<<endl;
ls3.remove("Mr");//deletes all a value "Mr"
itr3=ls3.begin();
while(itr3!=ls3.end())
{
cout<<*itr3<<"\t";
itr3++;
}


itr3=ls3.begin();
cout<<"Way 1: to erase single value by itr"<<endl;
list<string>::iterator itr4=ls3.begin();
itr3=ls3.erase(itr4);//1th element deleted
itr3=ls3.begin();
while(itr3!=ls3.end())
{
cout<<*itr3<<"\t";
itr3++;
}
cout<<"\nWay 2: to erase range by iterator"<<endl;
itr3=ls3.begin();
list<string>::iterator itr5=ls3.begin();
list<string>::iterator itr6=ls3.end();
itr3=ls3.erase(itr5,itr6);//deletes begin to end
while(itr3!=ls3.end())
{
cout<<*itr3<<"\t";
itr3++;
}
cout<<""<<endl;

return 0;
}
/*
10      100     20     
Mr      Mr      Mr      Mr      Mr     
Way 0: to remove by value
Mrs     Miss    Mrs     Way 1: to erase single value by itr
Miss    Mrs    
Way 2: to erase range by iterator
*/