prettify

Oct 17, 2016

C++11: lambda, bind, thread

#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
int main()
{
    // vector container stores threads
    std::vector<std::thread> workers;
    for (int i = 0; i < 5; i++) {
        workers.push_back(std::thread([i]() 
        {
            std::cout << i << std::endl;
        }));
    }

    std::for_each(workers.begin(), workers.end(), [](std::thread &t) 
    {
        t.join();
    });
    std::cout << "end\n";

    return 0;
}
11:21am - hi ran 35 lines of C++ (finished in 1.29s):
14 
0 
2 
 
3 
end 

C++ 11: array, auto, range based loop, enum

1. std::array is a static vector. The size of std::array can not be changed after it was compiled, thus is it often more efficient than std::vector.

std:array<int> nums={1,3,4,9};
for(auto i=nums.begin(); i!=nums.end(); ++i) cout << i << " ";

2. auto type and range based loop
std:array<int> nums={1,3,4,9};
for(auto& i: nums) cout << i << " ";

then range based loop almost can apply to any complicated type, e.g

map<int,string> m;
for(auto& x: m) cout<<x.first << ":" << x.second << endl;

pair<int,int> p;
for(auto& x: p) cout <<x.first <<":"<<x.second<<endl;

vector<int> a;
for(auto& x: a) cout << x << endl;

priority_queue<int> pq;
for(auto& x: pq) cout <<x<<endl;

3. enum class
enum class color {red, green, blue};
color c=color::green;


C++ 11: override

The C++ 11 have a new keyword: "override". Let me use an example to explain how it works.

#include 

class base
{
  public:
    virtual void f1()
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    };
    virtual void f2() final
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    virtual void f3()
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    void f4()
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    virtual void f5()
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
};

class child final: public base
{
  public:
    virtual void f1() override//(1)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    /*virtual void f2()override//(2)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }*/
    /*virtual void f2()(3)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }*/
    /*void f2()(4)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }*/
    virtual void f3()//(5)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    /*virtual void f4()override//(6)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }*/
    virtual void f4()//(7)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
    void f5() override//(8)
    {
      std::cout << __PRETTY_FUNCTION__ << std::endl;
    }
};

int main()
{
  std::cout << "base" << std::endl;
  base a;
  a.f1();
  a.f2();
  a.f3();
  a.f4();
  a.f5();

  std::cout << "child" << std::endl;
  child b;
  b.f1();
  b.f2();
  b.f3();
  b.f4();
  b.f5();

  std::cout << "base*" << std::endl;
  base* p = new child();
  p->f1();
  p->f2();
  p->f3();
  p->f4();
  p->f5();
}

Run and I got the following results
4:50pm - ran 90 lines of C++ (finished in 766ms):
base 
virtual void base::f1() 
virtual void base::f2() 
virtual void base::f3() 
void base::f4() 
virtual void base::f5() 
child 
virtual void child::f1() 
virtual void base::f2() 
virtual void child::f3() 
virtual void child::f4() 
virtual void child::f5() 
base* 
virtual void child::f1() 
virtual void base::f2() 
virtual void child::f3() 
void base::f4() 
virtual void child::f5() 


Here are the comments( refer to the child class code)
(1) explicitly override a base class virtual function, that's ok
(2) explicitly override a base class final virtual function, that's not allowed by compiler
(3) implicitly override a base class final virtual function, that's not allowed by compiler
(4) not allowed by compiler since the base f2() is final
(5) implicitly  override a base class virtual function, compiler won't point out the issue if the base virtual function does not exist
(6) override can not works with virtual function, compiling error
(7) declare f4() as virtual, this is modify, not overide
(8) no virtual required here

Oct 4, 2016

Some NVMe links

NVMe Compliance Test
https://github.com/nvmecompliance/tnvme

Linux driver for NVMe Compliance Suite
https://github.com/nvmecompliance/dnvme

Linux NVME command interface
https://github.com/linux-nvme/nvme-cli

NVM-Express user utilities and test programs
http://git.infradead.org/users/kbusch/nvme-user.git

NVME Windows driver (OFA)
http://www.nvmexpress.org/windows-driver-microsoft-inbox/

NVME windows command issuer
https://github.com/Miyavi-Chen/NVMe_CMD_issuer