mercredi 15 janvier 2020

Why can two threads with one lock use CPU more than 100%

I'm working on a multi-core machine and the OS is Ubuntu.

I write a C++11 code as below:

thread t1([](){
        while (true) {
            lock_guard<mutex> lock(g_mutex);
        }
        });
thread t2([](){
        while (true) {
            lock_guard<mutex> lock(g_mutex);
        }
        });

t1.join();
t2.join();

As you see, two thread, one lock. So the two thread will be assigned onto two core, this is for sure.

However, after executing this program, I use top to check its usage of resource.

I found that the usage of CPU was about 180%.

As my understanding, only one thread can be executed at any moment because of the lock. So why could it use CPU more than 100%?

Aucun commentaire:

Enregistrer un commentaire