Home > Backend Development > C++ > Why Do Multithreaded Programs Freeze Under Compiler Optimization?

Why Do Multithreaded Programs Freeze Under Compiler Optimization?

Linda Hamilton
Release: 2024-12-13 16:31:15
Original
873 people have browsed it

Why Do Multithreaded Programs Freeze Under Compiler Optimization?

Why Does a Multithreading Program Get Stuck in Optimized Mode?

This article explores an issue commonly encountered in multithreaded programs, where the program gets stuck in optimized mode (-O1, -O2, -O3) but behaves normally in unoptimized mode (-O0).

Consider the following multithreaded program written in C :

static bool finished = false;

int func() {
    size_t i = 0;
    while (!finished)
        ++i;
    return i;
}

int main() {
    auto result = std::async(std::launch::async, func);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    finished = true;
    std::cout << "result =" << result.get();
    std::cout << "\nmain thread>
Copy after login

When running this program in debug mode (unoptimized) or with GCC's -O0 flag, it typically behaves as expected and prints the result after 1 second. However, when compiled in release mode or with higher optimization levels (-O1, -O2, -O3), the program gets stuck and does not print anything.

The issue lies in the shared variable finished, which is non-atomic and non-guarded. The optimizing compiler reorders the memory access instructions, causing multiple threads to access this variable concurrently, leading to undefined behavior. To fix this, we should use an atomic variable for finished.

Here is the corrected code:

#include <iostream>
#include <future>
#include <atomic>

static std::atomic<bool> finished = false;

int func() {
    size_t i = 0;
    while (!finished)
        ++i;
    return i;
}

int main() {
    auto result = std::async(std::launch::async, func);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    finished = true;
    std::cout << "result =" << result.get();
    std::cout << "\nmain thread>
Copy after login

With this fix, the program will behave correctly even in optimized mode. It demonstrates the importance of using atomic variables in multithreaded programs to prevent data races and undefined behavior.

The above is the detailed content of Why Do Multithreaded Programs Freeze Under Compiler Optimization?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template