gdb attach
gdb attach
的用法:
#include <stdio.h>
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <chrono>
namespace {
class Queue {
public:
Queue() = default;
~Queue() { }
void Init(int num) {
for (int i = 0; i < num; ++i) {
queue_.push(i);
}
}
int Pop() {
std::unique_lock<std::mutex> lck(mutex_);
while (queue_.size() == 0) {
cv_.wait(lck);
}
int value = queue_.front();
queue_.pop();
return value;
}
void Push(int value) {
std::unique_lock<std::mutex> lck(mutex_);
queue_.push(value);
cv_.notify_all();
}
private:
std::queue<int> queue_;
std::mutex mutex_;
std::condition_variable cv_;
}; // class Queue
bool running = false;
void push(Queue& q) {
int value = 100;
while (running) {
q.Push(value++);
std::this_thread::sleep_for(std::chrono::minutes(100));
}
}
void pop(Queue& q) {
while (running) {
fprintf(stdout, "pop value: %d\n", q.Pop());
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
} // namespace
int main()
{
fprintf(stdout, "test start\n");
Queue q;
q.Init(2);
running = true;
std::thread th1(push, std::ref(q));
std::thread th2(pop, std::ref(q));
std::this_thread::sleep_for(std::chrono::seconds(10));
running = false;
th1.join();
th2.join();
fprintf(stdout, "test end\n");
return 0;
}