下面 C++ 代码的运行结果可能是()
#include <iostream>
#include <thread>
#include <mutex>
class Main {
private:
static int count;
std::mutex mtx;
public:
void increment() {
mtx.lock();
count++;
mtx.unlock();
}
static void main(int argc, char* argv[]) {
Main obj1, obj2;
std::thread t1([&obj1]() {
for (int i = 0; i < 1000; i++) {
obj1.increment();
}
});
std::thread t2([&obj2]() {
for (int i = 0; i < 1000; i++) {
obj2.increment();
}
});
t1.join();
t2.join();
std::cout << count << std::endl;
}
};
int Main::count = 0;
int main(int argc, char* argv[]) {
Main::main(argc, argv);
return 0;
}

