- 多个线程交替输出 (简易版)
#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
mutex lock1, lock2;
void f1()
{
while (true)
{
lock1.lock();
cout << "a";
lock2.unlock();
}
}
void f2()
{
while (true)
{
lock2.lock();
cout << "b";
lock1.unlock();
}
}
int main()
{
lock2.lock();
thread t1(f1);
thread t2(f2);
t1.join();
t2.join();
return 0;
}
- condtion varaible
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
using namespace std;
mutex m;
condition_variable cond;
int loop = 10;
int flag = 0;
void func(int id)
{
for (int i = 0; i < loop; ++i)
{
unique_lock<mutex> lk(m);
while (flag != id) cond.wait(lk);
cout << static_cast<char>('A' + id) << " ";
flag = (flag + 1) % 3;
cond.notify_all();
}
}
int main()
{
thread A(func, 0);
thread B(func, 1);
thread C(func, 2);
cout << endl;
A.join();
B.join();
C.join();
system("pause");
return 0;
}
- 读写锁
#include <iostream>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <thread>
using namespace std;
class rwlock {
private:
mutex _lock;
condition_variable _wcon, _rcon;
unsigned _writer, _reader;
int _active;
public:
void read_lock() {
unique_lock<mutex> lock(_lock);
++_reader;
while (_active < 0 || _writer > 0) _rcon.wait(lock);
--_reader;
++_active;
}
void write_lock() {
unique_lock<mutex> lock(_lock);
++_writer;
while (_active != 0) _wcon.wait(lock);
--_writer;
_active = -1;
}
void unlock() {
unique_lock<mutex> lock(_lock);
if (_active > 0) {
--_active;
if (_active == 0) _wcon.notify_one();
}
else {
_active = 0;
if (_writer > 0) _wcon.notify_one();
else if (_reader > 0) _rcon.notify_all();
}
}
rwlock() :_writer(0), _reader(0), _active(0) {}
};
void t1(rwlock* rw1) {
while (1) {
cout << "t1 I was to write." << endl;
rw1->write_lock();
cout << "t1 writing..." << endl;
this_thread::sleep_for(chrono::seconds(5));
rw1->unlock();
this_thread::sleep_for(chrono::seconds(5));
}
}
void t2(rwlock* rw2) {
while (1) {
cout << "t2 I want to read." << endl;
rw2->read_lock();
cout << "t2 reading..." << endl;
this_thread::sleep_for(chrono::seconds(1));
rw2->unlock();
}
}
void t3(rwlock* rw3) {
while (1) {
cout << "t3 I want to read." << endl;
rw3->read_lock();
cout << "t3 reading..." << endl;
this_thread::sleep_for(chrono::seconds(1));
rw3->unlock();
}
}
int main()
{
rwlock* rw = new rwlock();
thread th1(t1, rw);
thread th2(t2, rw);
thread th3(t3, rw);
th1.join();
th2.join();
th3.join();
return 0;
}
- 线程安全的STL queue
#include <mutex>
#include <condition_variable>
#include <deque>
#include <queue>
#include <iostream>
#include <memory>
#include <chrono>
#include <thread>
using namespace std;
template<typename DATATYPE, typename SEQUENCE = deque<DATATYPE>>
class ConcurrenceQueue {
public:
ConcurrenceQueue() = default;
ConcurrenceQueue(const ConcurrenceQueue& other) {
lock_guard<mutex> lg(other.mutex);
m_data = other.data;
}
ConcurrenceQueue(ConcurrenceQueue&& other) = delete;
~ConcurrenceQueue() = default;
bool empty() const {
lock_guard<mutex> lg(m_mutex);
return m_data.empty();
}
void push(const DATATYPE& data) {
lock_guard<mutex> lg(m_mutex);
m_data.push(data);
m_cond.notify_one();
}
void push(const DATATYPE&& data) {
lock_guard<mutex> lg(m_mutex);
m_data.push(std::move(data));
m_cond.notify_one();
}
shared_ptr<DATATYPE> try_pop() {
lock_guard<mutex> lg(m_mutex);
if (m_data.empty()) return {};
auto res = make_shared<DATATYPE>(m_data.front());
m_data.pop();
return res;
}
shared_ptr<DATATYPE> pop() {
unique_lock<mutex> lg(m_mutex);
m_cond.wait(lg, [this] { return !m_data.empty(); });
auto res = make_shared<DATATYPE>(move(m_data.front()));
m_data.pop();
return res;
}
private:
queue<DATATYPE, SEQUENCE> m_data;
mutable mutex m_mutex;
condition_variable m_cond;
};
ConcurrenceQueue<int> g_queue;
void producer() {
for (int i = 0; i < 100; ++i) {
g_queue.push(i);
this_thread::sleep_for(chrono::seconds(3));
}
}
void consumer1() {
while (1) {
printf("[1] ------- %d\n", *g_queue.pop());
}
}
void consumer2() {
while (1) {
auto front = g_queue.try_pop();
printf("[2] ------- %d\n", front ? *front : -1);
this_thread::sleep_for(chrono::seconds(1));
}
}
int main() {
thread t1(producer);
thread t3(consumer2);
t1.join();
t3.join();
}