Beginners thoughts on Client-Server/ Cloud!
Trivial references:
https://en.wikipedia.org/wiki/Zeus
https://en.wikipedia.org/wiki/Vajra
Trivial references:
https://en.wikipedia.org/wiki/Zeus
https://en.wikipedia.org/wiki/Vajra
typedef myLongLongMeaningfulType tMeaningfulType typedef std::map<int,std::map<int, std::vector<int> > > doubleIndexMap;
uint8_t add (uint8_t a, uint8_t b);
typedef uint8_t supportType_t; supportType_t add (supportType_t a, supportType_t b);
typedef uint16_t supportType_t;
typedef uint8_t length_t; // always stores lengths in uint8 format. typedef uint8_t area_t; // always stores area's in uint8 format.
typedef uint8_t length_t; typedef uint8_t area_t;
length_t m_boxLength = 10_CM;
area_t m_boxArea = 100_SQCM;
length_t getLength()
{
return m_boxArea; // Bug, no compiler warning. both area and length are uint8
}
if(box1.getLength() == box2.getArea()) // Bug, no compiler warning. both area and length are uint8
int X = 0XFF; // Nah! not possible int Y = 0888; // yeah, i got it right. Its a decimal 888 int Z = 0110; // Sadly, I'm still not upgraded to C++14 and i understand only decimal!!!
cMoney m1(10); // Do you think I'm worth 10USD? I'm worth 10INR. Check out my implementation! cMoney m2(10.5_inr); // We both know what I'm worth of :)
cMoney m1(10); // I'm worth 10INR. If you don't think so, init my worth in INR!!
cMoney m2(10.5_inr); cMoney m2(10.5_usd); oops, it should have been a different unit!
class cMoney
{
private:
long double value; // always store in USD
// Accept money only in acceptable currencies.
// The acceptable currencies will be defined by the user defined literals.
cMoney(long double money): value(money) {}// private, force strong types.
public:
cMoney() : value(0) {}
cMoney(const cMoney& money): value(money.value) {}
// define user defined literals.
// I will accept money only in the below currencies.
// And We both are sure how much money we are dealing with.
friend cMoney operator"" _usd(long double val);
friend cMoney operator"" _inr(long double val);
};
cMoney operator"" _usd(long double val)
{
return cMoney(val);
}
cMoney operator"" _inr(long double val)
{
return cMoney(CurrencyConversion::ConvertTo<CurrencyConversion::INR, CurrencyConversion::USD>(val));
}
//...
cMoney m1(10); // ERROR! private constructor
cMoney m2(10.5_inr); // public user defined literal creates the object with appropriate conversion and then copy creates the object.
void Hello(std::string who)
{
std::cout << "Hello " << who << "!" << std::endl;
}
int main()
{
std::string who("world");
std::thread myThread(Hello, who); // create myThread to run Hello with parameter who
// while myThread is busy saying Hello. I shall finish other tasks.
myThread.join(); // wait for myThread to complete
std::cout << "Thread finished saying Hello!";
return 0;
}
std::mutex protector; // mutex to be locked by the threads that contend for who.
std::string who; // shared data/ memory
void Hello()
{
protector.lock(); // lock the data so that main is not using it.
std::cout << "Hello " << who << "!" << std::endl;
protector.unlock(); // unlock the data so that main can continue using it.
}
int main()
{
protector.lock(); // lock the data so that Hello thread is not using it.
std::thread myThread(Hello); // create myThread to run Hello
who = "world";
protector.unlock(); // Unlock the data so that Hello thread can continue using it.
// while myThread is busy saying Hello. I shall finish other tasks.
myThread.join(); // wait for myThread to complete
std::cout << "Thread finished saying Hello!";
return 0;
}
std::mutex protector;
std::string who; // shared data
std::condition_variable conditionCheck;
bool isComplete = false;
void worker_thread()
{
// Wait until main() finishes its task.
protector.lock();
// Beware of spurious wake up. Check the notification variable to make sure the condition variable is signalled.
while(!isComplete) // If it is a spurious wake up, wait till main signals.
conditionCheck.wait(protector); // give up lock and wait for main to signal
// lock is automatically re accquired
std::cout << "Hello " << who << "!" << std::endl;
protector.unlock();
}
int main()
{
std::thread myThread(worker_thread);
protector.lock();
who = "world";
isComplete = true;
protector.unlock();
conditionCheck.notify_one();
myThread.join(); // wait for myThread to complete
std::cout << "Thread finished saying Hello!";
return 0;
}
void setVal(std::promise<int>& prom) {
prom.set_value (100);
}
int main ()
{
// Make a promise.
std::promise<int> prom;
// Promise to be fulfilled in the future.
std::future<int> fut = prom.get_future();
// My thread will fulfil the promise in the future.
std::thread myThread(setVal, std::ref(prom)); // Pass by reference so that it can set the value in the future obtained.
// Meanwhile, do other stuff.
// Get the value set by the promise or wait till it is set.
int val = fut.get(); // future is not even ref type, How does it have the value set by the promise!!!!
myThread.join();
return 0;
}
// Execute setVal asynchronously and return future for the return value. std::future<int> fut = std::async(std::launch::async, setVal); // How do we know the future type!! It is the same as the type as std::result_of setVal functon