以下代碼會出現棧溢出(stack overflow)
#include <stdio.h>
#include <thread>
#define MAX_SIZE 3145728
class A {
public:
A() :worker_(std::thread(&A::task, this)) {}
private:
void task() {
char buf[MAX_SIZE];
printf("running...");
while (1);
}
std::thread worker_;
};
int main() {
A a;
return 0;
}
原因是buf數組空間分配過大。
調試的時候會發現,連線程的第一步都不會執行,程序直接掛掉。
應當改成動態分配。