Programmer's Blog

Programmer's reference

Monthly Archives: May 2016

[Linux] Useful commands

Execute commands on files found by the find command
$ find -iname "MyCProgram.c" -exec md5sum {} \;

Find all empty files in home directory
# find ~ -empty

converts the DOS file format to Unix file format using sed command.
$sed 's/.$//' filename

Print only specific field from a file.
$ awk '{print $2,$5;}' employee.txt

Go to the 143rd line of file
$ vim +143 filename.txt

Open the file in read only mode.
$ vim -R /etc/passwd

[C++] check hardware concurrency

unsigned int n = std::thread::hardware_concurrency();

[C++] Scoped thread class

class scoped_thread
{
  std::thread t;
public:
  explicit scoped_thread(std::thread t_) : t(std::move(t_))
  {
    if(!t.joinable())
      throw std::logic_error(“No thread”);
  }

  ~scoped_thread()
  {
    t.join();
  }
  scoped_thread(scoped_thread const&)=delete;
  scoped_thread& operator=(scoped_thread const&)=delete;
};

[C++] RAII thread guard class

 

class thread_guard
{
  std::thread& t;
public:
  explicit thread_guard(std::thread& t_) : t(t_) {}
  ~thread_guard()
  {
    if(t.joinable())
    {
      t.join();
    }
  }
  thread_guard(thread_guard const&)=delete;
  thread_guard& operator=(thread_guard const&)=delete;
};

 

[Java] Simple thread-safe singleton class

public class threadSafeSingleton {

  //objects in singleton
  private HashMap<String, String> myHashMap = new HashMap<String, String>();
  private static threadSafeSingleton singletonInstance = null;

  protected threadSafeSingleton() {
  }
  
  public static threadSafeSingleton getSingleton() {
    if (singletonInstance == null) {
      singletonInstance = new threadSafeSingleton();
    }
    return singletonInstance;
  }

}