Saturday, July 14, 2012

Pthread in c++ class

In pthread API, the pthread_create() takes function which takes a generic pointer and returns a generic pointer. Here is a sample pthread implementation in c

Output:-
In thread....
Message : Hello
Thread exiting...
The problem with pthread_create in c++ is that it can't take member function as a thread function. Because, pthread_create expects a normal function, not a c++ member function. If we pass a member function, then the implicit this pointer won't be available inside this, hence the purpose of member function won't be fulfilled. For this reason, we need to make the thread function static and pass this pointer as object. Inside the function, everything should be accessed through the this pointer. Here is a sample code of pthread_create in C++

Output:-
In Thread...
Message : Hi Hi
Thread exiting...
Here, the problem is we need to write a static member function for every class that needs to use pthread_create(). I have made a wrapper class for this. Here is the code along with how to use it.

Output
Control in Main
In Thread...
Message : Ho Ho
Thread exiting...
 

Friday, April 27, 2012

Yahoo Finance API in Python 3.2


This is a sample code to fetch market data from yahoo finance.

Output :-
Unsorted :
Ask  =>  117.25
Bid  =>  116.25
Change  =>  -3.60
Trade Date  =>  -
52-week Low  =>  111.20
Change From 52-week Low  =>  +5.05
Percebt Change From 52-week High  =>  -48.28%
Open  =>  120.65
Change in Percent  =>  "-3.00%"
Ex-Dividend Date  =>  "14-Sep-11"
Day's Low  =>  115.55
52-week High  =>  224.75
Percent Change From 52-week Low  =>  +4.54%
Last Trade Size  =>  1
Previous Close  =>  119.85
Symbol  =>  "HINDALCO.NS"
Last Trade Time  =>  "6:29am"
Last Trade Date  =>  "4/26/2012"
Day's High  =>  121.15
Change From 52-week High  =>  -108.50
Last Trade (Price Only)  =>  116.25
Volume  =>  10257747
Stock Exchange  =>  "NSE"

Sunday, April 15, 2012

Recursive Merge Sort in C++



Output :-
Unsorted :
308  259  354  738  444  7  891  756  63  56  460  961  732  38  648
Sorted :
7  38  56  63  259  308  354  444  460  648  732  738  756  891  961