C++ 11

#include
#include
#include
#include<string.h>
#include
#include
using namespace std;

//class Point
//{
//public:
// Point(int x = 0, int y = 0)
// :_x(x)
// , _y(y)
// {
// cout << “X Y” << endl;
// }
//private:
// int _x;
// int _y;
//};

//int main()
//{/*
// Point p{ 1,2 };
// return 0;/
// //short a = 32670;
// //short b = 32670;
// //auto c = a + b;
// //cout << c << endl;
// int a = 10;
// int b = 20;
// decltype(a + b) c;
// cout << typeid©.name() << endl;
//}
//
//void
G(size_t size)
//{
// return malloc(size);
//}

//int main()
//{
// cout << typeid(decltype(G)).name() << endl;
// cout << typeid(decltype(G(0))).name() << endl;
// return 0;
//}
//
//int Add(int a, int b)
//{
// return a + b;
//}
//
//int main()
//{
// const int&& ra = 10;
// int&& r = Add(10, 20);
// cout << r << endl;
//}
//
//void Fun(int& x)
//{
// cout << “lvalue ref” << endl;
//}
//void Fun(int&& x)
//{
// cout << “rvalue ref” << endl;
//}
//void Fun(const int& x)
//{
// cout << “const lvalue ref” << endl;
//}
//void Fun(const int&& x)
//{
// cout << “const rvalue ref” << endl;
//}
//
//template
//void P(T&& t)
//{
// Fun(std::forward(t));
//}
//
//int main()
//{
// P(10);
// int a;
// P(a);
// P(std::move(a));
//}

//int main()
//{
// string s1(“hello”);
// string s2(“world”);
// string&& s3 = s1 + s2;
// cout << s3 << endl;
// return 0;
//}

//int main()
//{
// int array[] = { 4,3,2,1,6,7,8,9,0 };
// sort(array,array+sizeof(array)/sizeof(array[0]));
// for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++)
// {
// cout << array[i] << endl;
// }
// return 0;
//}

//struct Goods
//{
// string _name;
// double _price;
//};

//struct Compare
//{
// bool operator()(const Goods& g1, const Goods& gr)
// {
// return g1._price <= gr._price;
// }
//};
//
//int main()
//{
// Goods gds[] = { {“苹果,2.1”},{“香蕉”,3},{“橙子”,2.2} };
// sort(gds, gds + sizeof(gds) / sizeof(gds[0]), Compare());
// return 0;
//}

//int main()
//{
//
// Goods gds[] = { {“苹果,2.1”},{“香蕉”,3},{“橙子”,2.2} };
// sort(gds, gds + sizeof(gds) / sizeof(gds[0]),
// [](const Goods& l, const Goods& r)
// {
// return l._price < r._price;
// }
//);
// return 0;
//
//}

//int main()
//{
// //[] {};
//
// //int a = 3, b = 4;
// //auto fun1 = [=,&b](int c)->int{return b += a + c; };
// //fun1(10);
// //cout << a << " " << b << endl;
// int x = 10;
// auto add = [x](int a)mutable {x *= 2; return a + x; };
// cout << add(10) << endl;
// return 0;
//}

//class Rate
//{
//public:
// Rate(double rate)
// :_rate(rate)
// {
//
// }
// double operator()(double money, int year)
// {
// return money * _rateyear;
// }
//private:
// double _rate;
//};
//
//int main()
//{
// double rate = 0.49;
// //Rate r1(rate);
// //r1(10000, 2);
//
// auto r2 = [=](double monty, int year)->double {return monty * rate
year; };
// r2(10000, 2);
// return 0;
//}
//
//int main()
//{
// std::thread t1;
// cout << t1.get_id() << endl;
// return 0;
//}
//
//void T(int a)
//{
// cout << “Thread1” << a << endl;
//}
//
//class TF
//{
//public:
// void operator()()
// {
// cout << “Thread3” << endl;
// }
//};
//
//int main()
//{
// thread t1(T, 10);
//
// thread t2([] {cout << “Thread2” << endl; });
//
// TF tf;
// thread t3(tf);
//
// t1.join();
// t2.join();
// t3.join();
// cout << “Main thread!” << endl;
// return 0;
//}

//void T1(int& x)
//{
// x += 10;
//}
//
//void T2(int* x)
//{
// *x += 10;
//}
//
//int main()
//{
// int a = 10;
// thread t1(T1, a);
// t1.join();
// cout << a << endl;
//
// thread t2(T1, std::ref(a));
// t2.join();
// cout << a << endl;
// return 0;
//
//}
mutex m;
unsigned long sum = 0;
void fun(size_t num)
{
for (size_t i = 0; i < num; i++)
{
m.lock();
sum++;
m.unlock();
}
}

int main()
{
cout << “Before” << sum <<endl;
thread t1(fun, 10000000);
thread t2(fun, 10000000);
t1.join();
t2.join();

cout << "After" <<sum<< endl;
return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值