Showing posts with label Difference. Show all posts
Showing posts with label Difference. Show all posts

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.

Thursday, December 6, 2012

A classic difference between C & C++

In C++ it is strictly enforced that all functions must be declared before they are used. But in C this isn't necessary.

This code is valid C, but it is not valid C++.

#include <stdio.h>
int main()
{
    test();
    return 0;
}

int test()
{
    printf( "Hello C" );
}



     * invalid code *                * Valid code *                  * valid code *

#include <iostream>           #include <iostream>           #include <iostream>
using namespace std;          using namespace std;         using namespace std;
                                                                          
int main()                          int test();                          int test()
{                                                                            {
    test();                           int main()                          printf("hello cpp");
}                                      {                                     }
                                           test();                    
int test()                           }                                     int main()
{                                                                            {
    printf("Hello cpp");           int test()                            test();
}                                      {                                     }
                                           printf("hello CPP");   
                                        }                              

Wednesday, November 21, 2012

Something about Class And Struct in cpp

#include <iostream>
using namespace std;

class A{
    public:
        int a;
};

struct B:A{};

struct C{
    int c;
};

class D:C{};

int main()
{
    B b;
    D d;
    b.a = 1;
    d.c = 2;  //give error at this line
}

OUTPUT:

Something to Remember:
  • In c# struct goes on stack means they are value type. and class is reference type. So there is huge difference between struct and class in c# but in C++ there is almost no difference between a struct and  a class (beside that public and private thing).

Monday, June 18, 2012

Write a program to find the greatest among ten numbers

// program in c++
#include <iostream>
using namespace std;

int main()
{
    int a[10];
    int i;                //int i is used in loop
    cout << "\n This program in C++";
    cout << "\n Enter your 10 number: ";
   
    //store 10 numbers in an array
    for (i=0; i<10; i++){
        cin >> a[i];
    }
   
    //assume that a[0] is greatest
    int greatest;
    greatest = a[0];
   
    for (i=0; i<10; i++){
        if (a[i] > greatest){
            greatest = a[i];
        }
    }
   
    cout << "\n Greatest of 10 number is: " << greatest << "\n\n";
    return 0;
}

-----------------------------------------------------------------------------------------------
// program in C
#include <stdio.h>
#include <conio.h>

int main()
{
    int a[10];
    printf("\n This program in C");
    printf("\n Enter your 10 number: ");
   
    //store 10 numbers in an array
    for (int i=0; i<10; i++){               //here we declare int i in loop
        scanf ("%d",&a[i]);
    }
   
    //assume that a[0] is greatest
    int greatest;
    greatest = a[0];
   
    for (int i=0; i<10; i++){     //here we again declare int i in loop, because
        if (a[i] > greatest){       //we haven't declare like previous program
        greatest = a[i];
        }
    }
   
    printf ("\n Greatest of 10 number is: %d \n\n", greatest);
    return 0;
}


OUTPUT:

Friday, June 8, 2012

Difference between class and struct in cpp

1) Members of a class are private by default and members of struct are public by default. For example program 1 fails in compilation and program 2 works fine.

// Program 1 to show we can't access the class data directly
#include <iostream>
using namespace std;

class Demo 

{
    int x;         // x is private (by default)
};

int main()
{
   Demo d;
   d.x = 20;      // compiler error because x is private
   cout << d.x ;
}

// Program 2 to show we can access struct data directly
#include <iostream>
using namespace std;

struct Demo
{
    int x;       // x is public (by default)
};

int main()
{
  Demo d;
  d.x = 20;    // works fine because x is public
  cout << d.x ;
}


2) When deriving a struct from a class/struct, default access-specifier for a base class/struct is public. And when deriving a class, default access specifier is private. For example program 3 fails in compilation and program 4 works fine.

// Program 3 to show inheritance between classes is, private by-default
#include <iostream>
using namespace std;

class Base

{
   public:
      int x;
};

class Derived : Base { };  
  // is equilalent to class Derived : private Base {}

int main()
{
  Derived d;
  d.x = 20;        // compiler error becuase inheritance is private
  cout << d.x ;
}

// Program 4 to show inheritance between structure is, public by-default
#include <iostream>
using namespace std;

struct Base 

{
    public:
       int x;
};

struct Derived : Base { };   // is equilalent to struct Derived : public Base {}

int main()
{
   Derived d;
   d.x = 20;       // works fine becuase inheritance is public
   cout << d.x ;
}


Note: struct in c# doesn't  support inheritance.