#include <functional> #include <iostream> void f(int& n1, int& n2, const int& n3) { std::cout << "In function: n1[" << n1 << "] n2[" << n2 << "] n3[" << n3 << "]" << std::endl; ++n1; // 增加存储于函数对象的 n1 副本 ++n2; // 增加 main() 的 n2 //++n3; // 编译错误 std::cout << "In function end: n1[" << n1 << "] n2[" << n2 << "] n3[" << n3 << "]" << std::endl; } int main() { int n1 = 1, n2 = 1, n3 = 1; std::cout << "Before function: n1[" << n1 << "] n2[" << n2 << "] n3[" << n3 << "]" << std::endl; std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3)); bound_f(); std::cout << "After function: n1[" << n1 << "] n2[" << n2 << "] n3[" << n3 << "]" << std::endl; }
#include <iostream> #include <thread> #include <mutex> void inc(std::mutex &mutex, int loop, int &counter) { for (int i = 0; i < loop; i++) { mutex.lock(); ++counter; mutex.unlock(); } } int main() { std::thread threads[5]; std::mutex mutex; int counter = 0; for (std::thread &thr: threads) { thr = std::thread(inc, std::ref(mutex), 1000, std::ref(counter)); } for (std::thread &thr: threads) { thr.join(); } // 输出:5000,如果inc中调用的是try_lock,则此处可能会<5000 std::cout << counter << std::endl; return 0; }
原文:https://www.cnblogs.com/lovebay/p/11579909.html