The physical host has four cores, and my Qemu command line looks like this:
Code: Select all
qemu-system-x86_64 \
-machine type=q35,accel=kvm \
-cpu host \
-smp 4 \
-m 8G \
-bios /usr/share/edk2/ovmf/OVMF.inteltdx.fd \
-serial pty \
-drive file=./hd1.img,index=0,media=disk,format=raw \
-nic user,model=e1000,ipv6=off,hostfwd=tcp::2223-10.0.2.3:23
Code: Select all
$ show cpu
System: VMS1, QEMU Standard PC (Q35 + ICH9, 2009)
CPU ownership sets:
Active 0-3
Configure 0-3
CPU state sets:
Potential 0-3
Autostart 0-3
Powered Down None
Not Present None
Hard Excluded None
Failover None
Code: Select all
...
...
Thread 0xE9280 has done another 10,000,000 iterations.
Thread 0xE9280 has done another 10,000,000 iterations.
Thread 0xE9280 has done another 10,000,000 iterations.
Thread 0xD7280 has done another 10,000,000 iterations.
Thread 0xD7280 has done another 10,000,000 iterations.
Thread 0xD7280 has done another 10,000,000 iterations.
Thread 0x143280 has done another 10,000,000 iterations.
Thread 0x143280 has done another 10,000,000 iterations.
Thread 0x131280 has done another 10,000,000 iterations.
Thread 0x10D280 has done another 10,000,000 iterations.
Thread 0x10D280 has done another 10,000,000 iterations.
Thread 0x179280 has done another 10,000,000 iterations.
Thread 0x179280 has done another 10,000,000 iterations.
Thread 0x167280 has done another 10,000,000 iterations.
Thread 0x167280 has done another 10,000,000 iterations.
Thread 0x11F280 has done another 10,000,000 iterations.
Thread 0x11F280 has done another 10,000,000 iterations.
Thread 0x11F280 has done another 10,000,000 iterations.
...
...
Finally, here's the program source. Note that I would have preferred to attach the file to this post but the .cpp file type isn't permitted:
Code: Select all
#include <atomic>
#include <condition_variable>
#include <future>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
std::atomic_size_t g_NumThreadsRunning;
std::condition_variable g_CV;
std::mutex g_Mutex;
void WorkerThreadStart()
{
size_t result = 0;
for (size_t i = 0; i < 100'000'000; i++)
{
if (((i + 1) % 10'000'000) == 0)
{
std::cout
<< "Thread 0x"
<< std::this_thread::get_id()
<< " has done another 10,000,000 iterations."
<< std::endl;
}
if ((i % 17) == 0 && (i % 13) == 0)
{
result++;
}
}
std::unique_lock lock{ g_Mutex };
g_NumThreadsRunning--;
lock.unlock();
g_CV.notify_one();
}
int main()
{
std::vector<std::thread> workerThreads;
for (size_t i = 0; i < 10; i++)
{
g_NumThreadsRunning++;
workerThreads.emplace_back(WorkerThreadStart);
}
std::unique_lock lock{ g_Mutex };
g_CV.wait(lock, []{ return g_NumThreadsRunning == 0; });
for (auto& workerThread : workerThreads)
{
workerThread.join();
}
return 0;
}