Skip to content

Instantly share code, notes, and snippets.

@skyscribe
Created August 18, 2012 02:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skyscribe/3383906 to your computer and use it in GitHub Desktop.
Save skyscribe/3383906 to your computer and use it in GitHub Desktop.
C++11 examples
#include <cstdlib>
#include <iostream>
#include <thread>
using namespace std;
once_flag flag;
void someComplicatedThings()
{
call_once(flag, [](){
cout << "called only once " << endl;
});
std::cout << "Called each time!" << endl;
}
int main(int argc, const char *argv[])
{
thread t1(someComplicatedThings);
thread t2(someComplicatedThings);
thread t3(someComplicatedThings);
thread t4(someComplicatedThings);
thread t5(someComplicatedThings);
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
return 0;
}
cmake_minimum_required(VERSION 2.6)
if (use_llvm)
add_definitions(-std=c++11)
set(clang_version 3.1)
##llvm
set(CMAKE_CXX_COMPILER clang++)
#set(CMAKE_CXX_FLAGS "-std=c++11")
set(CMAKE_C_COMPILER clang)
set(CMAKE_AR llvm-ar-${clang_version})
set(CMAKE_LINKER llvm-ld-${clang_version})
set(CMAKE_NM llvm-nm-${clang_version})
set(CMAKE_OBJDUMP llvm-objdump-${clang_version})
set(CMAKE_RANLIB llvm-ranlib-${clang_version})
else()
add_definitions(-std=c++0x)
endif()
add_executable(for-range for-range.cpp)
add_executable(initializer-list initializer-list.cpp)
add_executable(type-interface type-interface.cpp)
add_executable(async-test async.cpp)
add_executable(call-once call_once.cpp)
add_executable(test-buf cond.cpp)
add_executable(lambda-test lambda.cpp)
foreach(tgt call-once async-test test-buf)
target_link_libraries(${tgt} pthread)
endforeach()
#include <cstdlib>
#include <mutex>
#include <thread>
#include <iostream>
#include <chrono>
using namespace std;
struct BoundedBuffer {
int* buffer;
int capacity;
int front;
int rear;
int count;
std::mutex lock;
std::condition_variable not_full;
std::condition_variable not_empty;
BoundedBuffer(int capacity) : capacity(capacity), front(0), rear(0), count(0) {
buffer = new int[capacity];
}
~BoundedBuffer(){
delete[] buffer;
}
void deposit(int data){
std::unique_lock<std::mutex> l(lock);
not_full.wait(l, [&count, &capacity](){return count != capacity; });
buffer[rear] = data;
rear = (rear + 1) % capacity;
++count;
not_empty.notify_one();
}
int fetch(){
std::unique_lock<std::mutex> l(lock);
not_empty.wait(l, [&count](){return count != 0; });
int result = buffer[front];
front = (front + 1) % capacity;
--count;
not_full.notify_one();
return result;
}
void fetchAllButLastOne(){
std::unique_lock<std::mutex> l(lock);
bool should_notify = (count == capacity);
while (count > 1){
front = (front + 1) % capacity;
--count;
}
if (should_notify){
not_full.notify_one();
}
}
};
int main(int argc, const char *argv[])
{
BoundedBuffer buf(12);
std::mutex exitMutex;
bool terminate = false;
//producer every 20 senc
std::thread producer([&](){
size_t i = 1;
bool exit;
while (true){
{
std::unique_lock<std::mutex> l(exitMutex);
exit = terminate;
}
if (!exit){
cout << "deposit " << i << endl;
buf.deposit(i++);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}else{
break;
}
}
});
std::thread consumer([&](){
for (size_t i = 0; i < 20; i++) {
cout << "Got data:" << buf.fetch() << endl;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
std::unique_lock<std::mutex> l(exitMutex);
cout << "exiting..." << endl;
terminate = true;
buf.fetchAllButLastOne();
});
producer.join();
consumer.join();
return 0;
}
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, const char *argv[])
{
//initializer list
std::vector<int> aVec = {1,2,3,4,5,6,7};
//range based for
for(auto v:aVec){
cout << v << endl;
}
//array
int arr[] = {1,2,3,4,5};
for(int& x : arr){
x *= x;
}
for(auto x : arr){
cout << x << ' ';
}
cout << endl;
}
#include <cstdlib>
#include <iostream>
#include <vector>
#include <initializer_list>
using namespace std;
class InitializerType {
public:
InitializerType (std::initializer_list<int> list) : m_data(list){}
virtual ~InitializerType (){};
//show the data
void show(){
size_t i = 0;
for(auto v : m_data){
cout << "m_data[" << i++ << "] = " << v << endl;
}
}
//reset data
void reset(std::initializer_list<int> list){
m_data = list;
}
private:
/* data */
std::vector<int> m_data;
};
//uniform initialization
struct DataCollection {
std::string m_name;
int m_id;
float m_price;
};
DataCollection getDefault(){
return {"test", 1, 1.0f};
}
using namespace std;
int main(int argc, const char *argv[])
{
//For non-POD class constructor
InitializerType obj = {1,2,3,4,5,6,7};
obj.show();
std::initializer_list<int> newv = {11,22,33,44,55};
cout << "reset as new value:";
obj.reset(newv);
obj.show();
cout << "uniform initialization using initialization list" << endl;
auto data = getDefault();
cout << "Data info:" << data.m_name << "," << data.m_id << "," << data.m_price << endl;
}
#include <cstdlib>
#include <iostream>
#include <vector>
#include <tr1/functional>
using namespace std;
using namespace std::tr1;
using std::tr1::placeholders::_1;
using std::tr1::placeholders::_2;
bool func(int a, double b, std::string c, int d){
cout << "called with:" << a << "," << b << "," << c << "," << d << endl;
return true;
}
int main(int argc, const char *argv[])
{
//auto for functor
auto newFunc = bind(&func, 1, 2.0, _1, _2);
int arg = 0;
newFunc("autoFunc", arg);
vector<int> aVec = {1,2,3,4};
size_t idx = 0;
for (auto it = aVec.begin(), itEnd = aVec.end(); it != itEnd; ++it){
cout << "aVec[" << idx << "]" << *it << endl;
}
//decltype
auto v = 1;
decltype(aVec[0]) b = v;
cout << "decltype b:" << b << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment