Old-old-old
- init.cpp
#include <iostream>
using namespace std;
class foo_c {
public:
uint i;
string s;
};
int main(int argc, char **argv) {
foo_c foo;
cout << foo.i << endl;
cout << foo.s << endl;
return 0;
}
Out
4294961080
- parentn.cpp
#include <iostream>
using namespace std;
class parent_t {
public:
parent_t() {
cout << "#parent init" << endl;
}
friend ostream& operator<<(ostream &out, const parent_t &p) {
return p.print(out);
}
virtual ostream& print(ostream& out) const {
out << "#parent out";
return out;
}
virtual void print1() {
cout << "#parent virt" << endl;
}
void print2() {
cout << "#parent nonvirt" << endl;
}
virtual ~parent_t() {
cout << "#parent destroy" << endl;
}
};
class child_t: public parent_t {
public:
child_t() {
cout << "#child init" << endl;
}
virtual ostream& print(ostream& out) const override {
out << "#child out";
return out;
}
virtual void print1() override {
cout << "#child virt" << endl;
}
void print2() {
cout << "#child novirt" << endl;
}
virtual ~child_t() {
cout << "#child destroy" << endl;
}
};
int main(int argc, char **argv) {
cout << "## start0" << endl;
child_t child;
//parent_t *parent;
//parent = &child;
//cout << *parent << endl;
//parent_t* parent = new parent_t;
//delete parent;
//parent = &child;
//cout << *parent << endl;
//cout << "#end" << endl;
child_t *child2;
child2 = &child;
cout << "## start1" << endl;
child2->print1();
child2->print2();
cout << "## start2" << endl;
parent_t *parent;
parent = &child;
parent->print1();
parent->print2();
cout << "##end" << endl;
return 0;
}
//$ ./parentn
//## start0
//#parent init
//#child init
//## start1
//#child virt
//#child novirt
//## start2
//#child virt
//#parent nonvirt
//##end
//#child destroy
//#parent destroy
Out
## start0
#parent init
#child init
## start1
#child virt
#child novirt
## start2
#child virt
#parent nonvirt
##end
#child destroy
#parent destroy
- ref.cpp
#include <iostream>
using namespace std;
int main(int argc, char **argv) {
int i = 1;
int& n = i;
int* m = &i;
i = 3;
cout << i << endl;
cout << n << endl;
cout << *m << endl;
cout << &i << endl;
cout << &n << endl;
cout << m << endl;
cout << &m << endl;
//int x=5, y=7, z=8;
//int arr[] {x, y, z}; // error: declaration of 'arr' as array of references
return 0;
}
Out
3
3
3
0x7fffffffe72c
0x7fffffffe72c
0x7fffffffe72c
0x7fffffffe718
- smartp.cpp
#include <iostream>
using namespace std;
class foo_t {
public:
foo_t() {
cout << "#foo init" << endl;
}
friend ostream& operator<<(ostream &out, const foo_t &p) {
return p.print(out);
}
virtual ostream& print(ostream& out) const {
out << "#foo out";
return out;
}
virtual void print() {
cout << "#foo out" << endl;
}
virtual ~foo_t() {
cout << "#foo destroy" << endl;
}
};
template <typename T>
class smart_pointer {
T *m_obj;
public:
smart_pointer(T *obj)
: m_obj(obj)
{ }
~smart_pointer() {
delete m_obj;
}
T* operator->() {
return m_obj;
}
T& operator* () {
return *m_obj;
}
};
void printer(foo_t *foo) {
foo->print();
}
void printer(smart_pointer<foo_t> foo) {
foo->print();
}
int main(int argc, char **argv) {
cout << "#start" << endl;
//auto foo = new foo_t;
//cout << *foo << endl;
//printer(foo);
smart_pointer<foo_t> foo_p(new foo_t());
printer(foo_p);
cout << "#end" << endl;
return 0;
}
/*
$ ./smartp
#start
#foo init
#foo out
#foo destroy
#end
#foo destroy
* /
Out
- smartp2.cpp
#include <iostream>
using namespace std;
class foo_t {
public:
foo_t() {
cout << "#foo init" << endl;
}
friend ostream& operator<<(ostream &out, const foo_t &p) {
return p.print(out);
}
virtual ostream& print(ostream& out) const {
out << "#foo out";
return out;
}
virtual void print() {
cout << "#foo out" << endl;
}
virtual ~foo_t() {
cout << "#foo destroy" << endl;
}
};
template <typename T>
class smart_pointer {
T *m_obj;
public:
smart_pointer(T *obj)
: m_obj(obj)
{ }
~smart_pointer() {
delete m_obj;
}
T* operator->() {
return m_obj;
}
T& operator* () {
return *m_obj;
}
};
void printer(foo_t *foo) {
foo->print();
}
void printer(smart_pointer<foo_t> foo) {
foo->print();
}
int main(int argc, char **argv) {
cout << "#start" << endl;
//auto foo = new foo_t;
//cout << *foo << endl;
//printer(foo);
smart_pointer<foo_t> foo_p(new foo_t());
//printer(foo_p);
cout << "#end" << endl;
return 0;
}
/*
$ ./smartp
#start
#foo init
#foo out
#foo destroy
#end
#foo destroy
*/
Out
#start
#foo init
#end
#foo destroy
- stream.cpp
#include <iostream>
using namespace std;
template <typename T>
class smart_pointer {
T *m_obj;
public:
smart_pointer(T *obj)
: m_obj(obj)
{ }
~smart_pointer() {
delete m_obj;
}
T* operator->() {
return m_obj;
}
T& operator* () {
return *m_obj;
}
};
//int test {
//smart_pointer<MyClass> pMyClass(new MyClass(/*params*/);
//pMyClass->something();
//std::cout << *pMyClass << std::endl;
//}
class parent_t {
public:
parent_t() {
cout << "#parent init" << endl;
}
friend ostream& operator<<(ostream &out, const parent_t &p) {
return p.print(out);
}
virtual ostream& print(ostream& out) const {
out << "#parent out";
return out;
}
~parent_t() {
cout << "#parent destroy" << endl;
}
};
class child_t: public parent_t {
public:
child_t() {
cout << "#child init" << endl;
}
virtual ostream& print(ostream& out) const override {
out << "#child out";
return out;
}
virtual ~child_t() {
cout << "#child destroy" << endl;
}
};
int main(int argc, char **argv) {
cout << "#start" << endl;
child_t child;
//parent_t *parent;
//parent = &child;
//cout << *parent << endl;
parent_t parent;
parent = child;
cout << parent << endl;
cout << "#end" << endl;
return 0;
}
Out
#start
#parent init
#child init
#parent init
#parent out
#end
#parent destroy
#child destroy
#parent destroy
- thpool.cpp
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <chrono>
using namespace std;
using namespace std::chrono;
using namespace std::this_thread;
class worker_t {
private:
mutex loop_mtx;
condition_variable loop_cv;
mutex exec_mtx;
condition_variable exec_cv;
atomic<bool> loop_ready{false};
atomic<bool> exec_ready{false};
atomic<bool> stop{false};
float input;
float result;
public:
float exec(float i) {
input = i;
loop_ready = true;
loop_cv.notify_one();
unique_lock<mutex> ul(exec_mtx);
while (!exec_ready.load()) exec_cv.wait(ul);
cout << "exec done" << endl;
exec_ready = false;
ul.unlock();
}
void kill() {
stop = true;
loop_ready = true;
loop_cv.notify_one();
}
void loop() {
while(true) {
unique_lock<mutex> ul(loop_mtx);
while (!loop_ready.load()) loop_cv.wait(ul);
if (stop.load()) return;
cout << "loop start" << endl;
result = i * i;
exec_ready = true;
exec_cv.notify_one();
loop_ready = false;
ul.unlock();
}
}
};
int main(int argc, char **argv) {
worker_t worker;
thread th1(&worker_t::loop, &worker);
auto f = [worker = &worker](){
//sleep_for(milliseconds(2000));
cout << "go" << endl;
worker->exec(1);
cout << "go" << endl;
worker->exec(2);
worker->kill();
};
thread th2(f);
th1.join();
th2.join();
return 0;
}
Out
- thread04.cpp
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
typedef struct {
string id;
string message;
} pth_args_t;
void *pth(void *args) {
auto pth_args = (pth_args_t*)args;
sleep_for(milliseconds(1000));
cout << "#pth " << pth_args->id << endl;
sleep_for(milliseconds(1000));
cout << "#pth " << pth_args->message << endl;
pthread_exit(0);
}
int main(int argc, char **argv) {
pthread_attr_t pth_attr;
pthread_t pth_id;
pth_args_t pth_args;
pth_args.id = "001";
pth_args.message = "hi";
pthread_attr_init(&pth_attr);
pthread_create(&pth_id, &pth_attr, pth, (void *)&pth_args);
pthread_join(pth_id, NULL);
return 0;
}
Out
#pth 001
#pth hi
- thread05.cpp
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
class pth_args_c {
private:
string id = "";
string msg = "";
mutex msg_mutex;
public:
pth_args_c(string id) {
this->id = id;
};
string get_id() {
return this->id;
}
void set_msg(string msg) {
this->msg = msg;
}
string get_msg() {
return this->msg;
}
};
void *pth(void *args) {
auto pth_args = (pth_args_c*)args;
sleep_for(milliseconds(1000));
cout << "#pth " << pth_args->get_id() << endl;
sleep_for(milliseconds(1000));
cout << "#pth " << pth_args->get_msg() << endl;
pthread_exit(0);
}
int main(int argc, char **argv) {
pthread_attr_t pth_attr;
pthread_t pth_id;
pth_args_c pth_args("01");
pth_args.set_msg("hi");
pthread_attr_init(&pth_attr);
pthread_create(&pth_id, &pth_attr, pth, (void *)&pth_args);
pthread_join(pth_id, NULL);
return 0;
}
Out
#pth 01
#pth hi
- thread06.cpp
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
class pth_args_c {
private:
string id = "";
string msg = "";
mutex msg_mutex;
public:
pth_args_c(string id) {
this->id = id;
};
string get_id() {
return this->id;
}
void set_msg(string msg) {
const lock_guard<mutex> lock(this->msg_mutex);
this->msg = msg;
}
string get_msg() {
return this->msg;
}
};
void pth1(pth_args_c &pth_args) {
sleep_for(milliseconds(5000));
cout << "#pth " << pth_args.get_id() << endl;
sleep_for(milliseconds(5000));
cout << "#pth " << pth_args.get_msg() << endl;
}
void pth2(pth_args_c &pth_args) {
sleep_for(milliseconds(1000));
cout << "#pth " << pth_args.get_id() << endl;
sleep_for(milliseconds(1000));
cout << "#pth " << pth_args.get_msg() << endl;
}
int main(int argc, char **argv) {
pth_args_c pth1_args("01");
pth1_args.set_msg("hi1");
pth_args_c pth2_args("02");
pth2_args.set_msg("hi2");
thread pthr1(pth1, ref(pth1_args));
thread pthr2(pth2, ref(pth2_args));
pthr1.join();
cout << "#wait" << endl;
pthr2.join();
return 0;
}
//EOF
Out
#pth 02
#pth hi2
#pth 01
#pth hi1
#wait
- thread07.cpp
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
#include <vector>
#include <memory>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
class worker_t {
private:
int id = 0;
string msg = "";
mutex msg_mutex;
public:
worker_t(int id) {
this->id = id;
};
int get_id() {
return this->id;
}
void set_msg(string msg) {
const lock_guard<mutex> lock(this->msg_mutex);
this->msg = msg;
}
void prn_msg() {
const lock_guard<mutex> lock(this->msg_mutex);
cout << "#pth "
<< this->id
<< " "
<< this->msg
<< endl;
}
string get_msg() {
return this->msg;
}
virtual ~worker_t() {
cout << "#worker destroy" << endl;
}
};
void pth(shared_ptr<worker_t> worker) {
//sleep_for(milliseconds(500));
worker->prn_msg();
}
int main(int argc, char **argv) {
vector<thread> ptrs;
vector<shared_ptr<worker_t>> workers;
for (auto i: { 1,2,3,4,5 }) {
auto worker = make_shared<worker_t>(i);
workers.push_back(worker);
}
for (auto worker: workers) {
ptrs.push_back(thread(pth, worker));
}
for (auto &thread: ptrs) {
thread.join();
}
cout << "#wait" << endl;
return 0;
}
//EOF
Out
#pth #pth 1#pth 3#pth
5
#pth 4
2
#wait
#worker destroy
#worker destroy
#worker destroy
#worker destroy
#worker destroy
- thread08.cpp
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
#include <vector>
#include <memory>
#include <chrono>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
void pthread(float value, shared_ptr<float> result) {
*result = value * value;
}
int main(int argc, char **argv) {
vector<thread> threads;
vector<shared_ptr<float>> results;
auto start = high_resolution_clock::now();
for (int i = 1; i < 1000; i++) {
auto result = make_shared<float>();
results.push_back(result);
}
float i = 1;
for (auto result: results) {
threads.push_back(thread(pthread, i++, result));
}
for (auto &thread: threads) {
thread.join();
}
for (auto result: results) {
//cout << *result << endl;
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "#wait " << duration.count() << endl;
return 0;
}
//EOF
Out
#wait 111811
- twork2.cpp
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <atomic>
#include <chrono>
using namespace std;
using namespace std::chrono;
using namespace std::this_thread;
class worker_t {
private:
mutex loop_mtx;
condition_variable loop_cv;
mutex exec_mtx;
condition_variable exec_cv;
atomic<bool> loop_ready{false};
atomic<bool> exec_ready{false};
atomic<bool> stop{false};
float input;
float result;
public:
float exec(float i) {
input = i;
loop_ready = true;
loop_cv.notify_one();
unique_lock<mutex> ul(exec_mtx);
while (!exec_ready.load()) exec_cv.wait(ul);
cout << "exec done" << endl;
exec_ready = false;
ul.unlock();
}
void kill() {
stop = true;
loop_ready = true;
loop_cv.notify_one();
}
void loop() {
while(true) {
unique_lock<mutex> ul(loop_mtx);
while (!loop_ready.load()) loop_cv.wait(ul);
if (stop.load()) return;
cout << "loop start" << endl;
result = i * i;
exec_ready = true;
exec_cv.notify_one();
loop_ready = false;
ul.unlock();
}
}
};
int main(int argc, char **argv) {
worker_t worker;
thread th1(&worker_t::loop, &worker);
auto f = [worker = &worker](){
//sleep_for(milliseconds(2000));
cout << "go" << endl;
worker->exec(1);
cout << "go" << endl;
worker->exec(2);
worker->kill();
};
thread th2(f);
th1.join();
th2.join();
return 0;
}
Out
- twork3.cpp
#include <iostream>
#include <mutex>
#include <vector>
#include <thread>
using namespace std;
class exec_t {
private:
mutex exec_mtx;
public:
float exec(float i) {
lock_guard<mutex> lock(exec_mtx);
auto res = i * i;
cout << res << endl;
return res;
}
};
void starter(exec_t& exec, float i) {
exec.exec(i);
}
int main(int argc, char **argv) {
exec_t exec;
vector<thread> threads;
for (auto i: { 1,2,3,4,5,6,7,8 }) {
threads.push_back(thread(starter, ref(exec), (float)i));
}
for (auto &thread: threads) {
thread.join();
}
return 0;
}
Out