我作出更大的計劃,我想使首先我試圖找出如何線程實際工作,所以我創建了一個簡單的程序來使用線程,我不明白爲什麼我沒有得到消息比"The bomb has just detonated."
"Bomberman has just put a bomb. Wait for the boom."
更早。C++線程無法正常工作?
任何人都可以指導我,爲什麼它不按我期望的那樣工作?
#include <unistd.h>
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
class CBomberman;
class CBomb {
public:
CBomb(void) : m_Owner(nullptr) {
thread other_thread(&CBomb::Timer, this);
other_thread.join();
}
private:
void Timer(void) {
this_thread::sleep_for(chrono::seconds(3));
cout << "The bomb has just detonated." << endl;
}
CBomberman* m_Owner;
};
class CBomberman {
public:
CBomberman(void) : m_Bomb(nullptr) {}
bool PutBomb(void) {
if (m_Bomb == nullptr) {
m_Bomb = new CBomb();
delete m_Bomb;
return true;
} else {
cout << "The bomb has already been put." << endl;
return false;
}
}
private:
CBomb* m_Bomb;
};
int main() {
CBomberman bomberman;
bomberman.PutBomb();
cout << "Bomberman has just put a bomb. Wait for the boom." << endl;
return 0;
}
請修復您發佈的代碼的格式。 – PaulMcKenzie
@PaulMcKenzie我不知道你在說什麼。 – scarface
爲什麼函數聲明中所有額外的空白? – PaulMcKenzie