Monday, January 28, 2013

Program to point some thing without using semicolon

#include <stdio.h>

int main()
{

    if (printf("hello world\n"))
    {
        //do nothing
    }
   
}


OUTPUT:

Friday, January 25, 2013

A note on Command line argument

Arguments can be passed to any function in a program, including the main() function.

Arguments passed to main(), must be declared as part of the function’s definition. To standardize passing arguments to a main() function, only two  items are allowed: a number and an array. The number is an integer variable, typically named argc (short for argument counter), and the array is a one-dimensional list, which is, by convention, named argv (short for argument values). Figure D.3 illustrates these two arguments.
----------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    int i;
   
    cout << "\n The number of items on the command line is "
         << argc << endl << endl;
       
    for(i = 0; i < argc; i++)
    {
        cout << "the address stored in argv[" << i << "] is "
             << int (argv[i]) << endl; //dispaly address as an integer number
           
        cout << "the character pointed to is " << *argv[i] << endl;
    }
   
    return 0;
}

OUTPUT:
 ____________________________________________________________

 // A program that displays its command-line arguments

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    int i;
    cout << "\nThe following arguments were passed to main(): ";
   
    for (i = 0; i < argc; i++)
    {
        cout << argv[i] << " ";
    }
   
    cout << endl;
    return 0;
}

OUTPUT:
 

Understand pointer to a pointer (**)

// program illustrating the pointer to pointer (**)
#include <iostream>
using namespace std;

int main()
{
    int i = 3;
    int *j;
    int **k;
   
    j = &i;
    k = &j;
   
    cout << "\n Address of i: " << &i << endl;
    cout << "\n Address of i: " << j << endl;
    cout << "\n Address of i: " << *k << endl;
    cout << "\n Address of j: " << &j << endl;
    cout << "\n Address of j: " << k << endl;
    cout << "\n Address of k: " << &k << endl;
   
    cout << "\n value of i: " << j << endl;
    cout << "\n value of k: " << k << endl;
    cout << "\n value of i: " << i << endl;
    cout << "\n value of i: " << *(&i) << endl;
    cout << "\n value of i: " << *j << endl;
    cout << "\n value of i: " << **k << endl;
    return 0;
}

OUTPUT:
Pointer terminology:
    int a = 12;
    int *b;
    b = &a;
   
    1. b contains address of an int
    2. value at address contained in b is an int
    3. b is an int pointer
    4. b points to an int
    5. b is a pointer which points in the direction of an int

-----------------------------------------------------------------------------------------------

// Function returning pointer
#include <iostream>
using namespace std;

int *fun();  //fun declaration

int main()
{
    int *p;
    p = fun();
    cout << "\n" << p << endl;
    cout << *p << endl;
    return 0;
}

int *fun()
{
    int i = 20;
    return(&i);
}

OUTPUT:


Monday, January 21, 2013

Write a program to swap 2 numbers using bitwise operators

//Write a program to swap 2 number without arithmetic operator

#include <stdio.h>

int main()
{
    int i=65;
    int k=120;
   
    printf("\n value of i=%d k=%d before swapping",i,k);
    i = i^k;
    k = i^k;
    i = i^k;
   
    printf("\n value of i=%d k=%d after swapping",i,k);
    return 0;
}


OUTPUT:

Thursday, January 17, 2013

Sorting Algorithm

We are going to discuss :
  1. Insertion
  2. Selection
  3. Bubble
  4. Shell
  5. Quick
  6. Merge
  7. Heap
void insertionSortArray(int arr[], int SIZE)
{
    int i, j, value, done;
    for(i = 0; i < SIZE; i++)
    {
        value = arr[i];
        j = i - 1;
        done = 0;
        do
        {
            if(arr[j] > value)
            {
                arr[j + 1] = arr[j];
                j--;
                if(j < 0) done = 1;
            }
            else done = 1;
        } while(!done);
        arr[j + 1] = value;
    }
}

/* ************************************ */
void selectionSortArray(int arr[], int SIZE)
{
    int min, i, j, tmp;
    for(i = 0; i < SIZE; i++)
    {
        min = i;
        for(j = i + 1; j < SIZE; j++)
            if(arr[j] < arr[min]) min = j;
            
        tmp = arr[i];
        arr[i] = arr[min];
        arr[min] = tmp;
    }
}

/* ************************************ */
void bubbleSortArray(int arr[], int SIZE)
{
    int i, tmp;
    for (i = 0; i < SIZE; i++)
    {
        if((i < SIZE - 1) && (arr[i] > arr[i + 1]))
        {
            tmp = arr[i];
            arr[i] = arr[i + 1];
            arr[i + 1] = tmp;
            i = -1;
        }
    }        
}

/* ************************************ */
void shellSortArray(int arr[], int SIZE)
{
    int i, j, k, m, mid;
    for(m = SIZE / 2; m > 0; m = m / 2)
    {
        for(j = m; j < SIZE; j++)
        {
            for(i = j - m; i >= 0; i = i - m)
            {
                if(arr[i + m] >= arr[i]) break;
                else
                {
                    mid = arr[i];
                    arr[i] = arr[i + m];
                    arr[i + m] = mid;
                }
            }
        }
    }
}

Wednesday, January 16, 2013

Smart Pointer (works as garbage collection) in c++

//smart (auto) pointer is a new feature of c++11
#include <memory>           //     <-- 0
#include <string>
using namespace std;

typedef auto_ptr<Car> CarPtr;


class Car{
    public:
        void startEngine()

        {
            //...................
        }
       
        void tuneRadioTo()

        {
            //...................
        }
};

void f()
{
  CarPtr p(new Car());         //    <-- 1
  p->startEngine();             //    <-- 2
  p->tuneRadioTo();            //    <-- 3
}                                    //    <-- 4

int main()
{
   f();
}
 

0: This gets the definition for auto_ptr
1: Create an object
2: Call a member function
3: Call another member function
4: Destroy the Car object

-----------------------------------------------------------------------------------------------

//Program to show drawback of auto pointer
#include <memory>             
#include <iostream>
using namespace std;

class Test{
    public:
        void print(){
            cout << "Test::print" << endl;
        }
};

int main(){
    auto_ptr<Test>  aptr1(new Test);
    aptr1 -> print();
   
    cout << aptr1.get();
   
    auto_ptr<Test>  aptr2(aptr1);
    aptr2 -> print();
   
    cout << aptr1.get();
    cout << aptr2.get();
   
    return 0;

}

OUTPUT:

To overcome this c++11 provide another mechanism called smart (shared) pointer.

Tuesday, January 15, 2013

A note on garbage value, memory allocation & variable auto intialization

// variable are global
#include <iostream>
using namespace std;

    int x,y,z,c,x2,x3;
    //y = x;
    //z = y;       // This gives error
    //c = z;

    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;

int main()
{
    y = x;
    z = y;
    c = z;
   
    cout << "\n" ;
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    cout << "\n\n";
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    return 0;
}


OUTPUT:

 ----------------------------------------------------------------------------------------------
// variable initialization all in main not global
#include <iostream>
using namespace std;

int main()
{
    int x,y,z,c,x2,x3;
    y = x;
    z = y;
    c = z;
   
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
    cout << "\n\n\n " << endl;
   
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
   
    return 0;
}


OUTPUT:

 ----------------------------------------------------------------------------------------------
 // variables in function
#include <iostream>
using namespace std;

void test()
{
    int x,y,z,c,x2,x3;
    y = x;
    z = y;
    c = z;
   
    cout << " value at x2: " << x2 << endl;
    cout << " value at x3: " << x3 << endl;
    cout << " value at  x: " << x << endl;
    cout << "Address of x: " << &x << endl;
    cout << " value at  y: " << y << endl;
    cout << "Address of y: " << &y << endl;
    cout << " value at  z: " << z << endl;
    cout << "Address of z: " << &z << endl;
    cout << " value at  c: " << c << endl;
    cout << "Address of c: " << &c << endl;
   
    int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;
    cout << "\n\n\n " << endl;
   
    cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
    cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
    cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
    cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
    cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
    cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
    cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
    cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
    cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
    cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
    cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
    cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
    cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
    cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
    cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
    cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
}

int main()
{
    test();
    return 0;
}


OUTPUT:

-----------------------------------------------------------------------------------------------
// variable in class ; using function ; object on STACK
#include <iostream>
using namespace std;

class Demo{
    public:
        int x,y,z,c,x2,x3;
        int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;  
   
    void test()
    {
        y = x;
        z = y;
        c = z;
       
        cout << "\n" ;
        cout << " value at x2: " << x2 << endl;
        cout << " value at x3: " << x3 << endl;
        cout << " value at  x: " << x << endl;
        cout << "Address of x: " << &x << endl;
        cout << " value at  y: " << y << endl;
        cout << "Address of y: " << &y << endl;
        cout << " value at  z: " << z << endl;
        cout << "Address of z: " << &z << endl;
        cout << " value at  c: " << c << endl;
        cout << "Address of c: " << &c << endl;
   
        cout << "\n\n";
        cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
        cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
        cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
        cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
        cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
        cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
        cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
        cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
        cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
        cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
        cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
        cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
        cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
        cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
        cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
        cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    }
};

int main()
{
    Demo d;
    d.test();
    return 0;
}


OUTPUT:

-----------------------------------------------------------------------------------------------
// variable in class ; using function ; object on HEAP
#include <iostream>
using namespace std;

class Demo{
    public:
        int x,y,z,c,x2,x3;
        int a,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15;   
   
    void test()
    {
        y = x;
        z = y;
        c = z;
       
        cout << "\n" ;
        cout << " value at x2: " << x2 << endl;
        cout << " value at x3: " << x3 << endl;
        cout << " value at  x: " << x << endl;
        cout << "Address of x: " << &x << endl;
        cout << " value at  y: " << y << endl;
        cout << "Address of y: " << &y << endl;
        cout << " value at  z: " << z << endl;
        cout << "Address of z: " << &z << endl;
        cout << " value at  c: " << c << endl;
        cout << "Address of c: " << &c << endl;
   
        cout << "\n\n";
        cout << "Address of  a: " << &a    << "   and VALUE: " << a << endl;
        cout << "Address of a1: " << &a1   << "   and VALUE: " << a1 << endl;
        cout << "Address of a2: " << &a2   << "   and VALUE: " << a2 << endl;
        cout << "Address of a3: " << &a3   << "   and VALUE: " << a3 << endl;
        cout << "Address of a4: " << &a4   << "   and VALUE: " << a4 << endl;
        cout << "Address of a5: " << &a5   << "   and VALUE: " << a5 << endl;
        cout << "Address of a6: " << &a6   << "   and VALUE: " << a6 << endl;
        cout << "Address of a7: " << &a7   << "   and VALUE: " << a7 << endl;
        cout << "Address of a8: " << &a8   << "   and VALUE: " << a8 << endl;
        cout << "Address of a9: " << &a9   << "   and VALUE: " << a9 << endl;
        cout << "Address of a10: " << &a10 << "  and VALUE: " << a10 << endl;
        cout << "Address of a11: " << &a11 << "  and VALUE: " << a11 << endl;
        cout << "Address of a12: " << &a12 << "  and VALUE: " << a12 << endl;
        cout << "Address of a13: " << &a13 << "  and VALUE: " << a13 << endl;
        cout << "Address of a14: " << &a14 << "  and VALUE: " << a14 << endl;
        cout << "Address of a15: " << &a15 << "  and VALUE: " << a15 << endl;
    }
};

int main()
{
    Demo *d = new Demo();
   
    d->test();
   
    delete d;
    d = 0;
    return 0;
}


OUTPUT:

Sunday, January 13, 2013

Virtual Inheritance

Virtual inheritance:
                        Inheritance can be private (where even the public fields of the base class are treated as private), public (where the base class determines visibility), or virtual. With virtual inheritance there is only one copy of each object even if (because of multiple inheritance) the object appears more than once in the hierarchy. In the following example, derived1a and derived1b each contain a base object, butderived2 contains just one field.

#include <iostream>
using namespace std;

class base{
    public:
        int i;
};

class derived1a: virtual public base {
    public:
        int j;
};

class derived1b: virtual public base {
    public:
        int k;
};

class derived2: public derived1a, public derived1b{
    public:
        int product(){
            return i*j*k;
        }
};

int main()
{
    derived2 obj;
    obj.i = 10;
    obj.j = 3;
    obj.k = 5;
   
    cout << "\n product is " << obj.product() << '\n';
    return 0;
}

OUTPUT:

Saturday, January 5, 2013

C++11 new feature Range based loop

//new c++11 standard feature

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int ia[] = {1,2,3,4,5,6,7,8,9,10};


    vector<int> iv(ia, ia + (sizeof(ia) / sizeof(int)));
   
    for ( int i:ia )

    {
        cout << i << " ";
    }
    cout << endl;
   
    for(int i:iv){
        cout << i << " ";
    }
    cout << endl;
   
    return 0;
}


OUTPUT:



Thursday, January 3, 2013

Simple Thread program using, Thread support library in c++

//thread is a new feature of c++11 standard

#include <iostream>
#include <thread>
using namespace std;

void threadFun()
{
    cout << "hi from thread \n";
}

int main()
{
    thread th(&threadFun);
    cout << "hi from main \n";
    th.join();
    return 0;
}


 

//this program to show parallel programming
// using lamda function


#include <iostream>
#include <thread>
using namespace std;

int main()
{
    thread th([]()
    {
        cout << "hi from thread \n";
    }
    );


    cout << "hi from main \n";
    th.join();
    return 0;
}


Run these program and see the beauty of thread. (most sexiest output you have ever seen before.)

Note: For thread library support in c++ you require latest complier
  • In linux GCC 4.6 or higher
  • In windows Visual c++ 2012 or MinGw64, (MinGw32 yet not able to compile thread program)

for further information see Thread support library.

Tuesday, January 1, 2013

What is threading

What is a thread:
  • A thread is the entity with in a process that can be scheduled for execution.
  • Threads are much like lightweight processes.
  • Allow the program to execute sequential actions or many actions at once.
  • All the threads in a process share the same address space.
  • It's a small independent chunk of code that can run concurrently with other threads.
  • Each thread runs independently of the others.
  • Each thread may run a different sequence of instructions.
  • A thread isn't a whole program.
  • Each process has atleast one thread.
Note: It’s important to note that a thread can do anything a process can do. But since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process.

What is a process:
  • An executing instance of a program is called a process.
  • Program under execution is known as process.
  • A process is always stored in the main memory also termed as the primary memory or random access memory.
  • Therefore, a process is termed as an active entity. It disappears if the machine is rebooted.
  • Each process provides the resources needed to execute a program.
  • Several process may be associated with a same program.
  • A process has a virtual address space, executable code, open handles to system objects, a security context, a unique process identifier, environment variables, a priority class, minimum and maximum working set sizes, and at least one thread of execution.
  • Each process is started with a single thread, often called the primary thread, but can create additional threads from any of its threads.

What is the difference between Process and Thread :
  1. Threads are easier to create than processes since they don't require a separate address space.  
  2. Multithreading requires careful programming since threads share data strucures that should only be modified by one thread at a time. Unlike threads, processes don't share the same address space.
  3. Threads are considered lightweight because they use far less resources than processes.
  4. Processes are independent of each other. Threads, since they share the same address space are interdependent, so caution must be taken so that different threads don't step on each other. This is really another way of stating 2 above.
  5. A process can consist of multiple threads.
  6. Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.
  7. Both threads and processes are atomic units of OS resource allocation.

What is Threading (or multithreading):
  • Multithreading is the ability of a program or process to manage multiple requesets by the same user at same time without having to have multiple copies of the program (that is running).
  • If a program have multiple thread that kind of program known as multithreading program.
  • Threads allows for multithreading.

Advantage of threading:
There are 2 main reasons to use threading in an application.
  • Multithreading improve application performance.
  • Separation of concerns.

When not to use multithreading:
  • The only reason not to use threading is when the benefit is not worth the cost.
  • The performance gain might not be as large as expected.

Disadvantage of threading:
  • It can complicate the code, making it harder to understand and more prone to bugs. Therefore it’s only worth doing for those performance-critical parts of the application where there’s the potential for measurable gain.

Why we use Threading:
  • In simple, to achieve concurrency.

What is concurrency:
  • Concurrency is about two or more separate activities happening at the same time.
  • In term of computers, a single system performing multiple independent activities in parallel, rather than sequentially, or one after the other.
  • Concurrency meaning in plain English -->Acting together, as agents, circumstances or events.

Why use concurrency:
The almost same reason that for threading.
  • Separation of concerns.
  • Performance.
           
Explaination for separation of concerns:
Separation of concerns is almost always a good idea when writing software; by grouping related bits of code together and keeping unrelated bits of code apart, you can make your programs easier to understand and test, and thus less likely to contain bugs. You can use concurrency to separate distinct areas of functionality, even when the operations in these distinct areas need to happen at the same time; without the explicit use of concurrency you either have to write a task-switching framework or actively make calls to unrelated areas of code during an operation.
Example:
A DVD player application for a desktop computer. Such an application fundamentally has two sets of responsibilities: not only does it have to read the data from the disk, decode the images and sound, and send them to the graphics and sound hardware in a timely fashion so the DVD plays without glitches, but it must also take input from the user, such as when the user clicks Pause or Return To Menu, or even Quit. In a single thread, the application has to check for user input at regular intervals during the playback, thus conflating the DVD playback code with the user interface code.

c++11 standard have the support of multithreaded programs.
c++11 standard provide components in the library for writing multithreaded application and it is known as 'thread'.
we achieve threading before c++11 standard.
what's the difference made by thread library in c++11 standard. (or why use thread library)
Well answer is: This will make it possible to write multithreaded c++ programs without relying on plateform-specific extensions and thus allow writing portable multithreaded code with guaranteed behavior.

What is Mutexes: (mighty)
  • Mutex stand for mutual exclusion and is the simple and most useful synchronization structure.
  • In essence it is a boolean that determines if an object is locked from use or not.

What is Semaphores: (super)
  • Semaphores are objects that allow a certain number of threads to access a certain object before it begins blocking it.
  • A semaphore with a value 2 will allow the first 2 threads in the queue access to it then block any threads after that.
  • A mutex is in essence just a semaphore that allows one thread at a time to access an object.