Sobes.tech
Junior — Middle

How confident are you in your understanding of multi-threaded programming concepts and practices?

sobes.tech AI

Answer from AI

I am familiar with the basic concepts of multithreaded programming, such as creating and managing threads, synchronization using mutexes and condition variables, preventing data races and deadlocks. In C/C++, I use the standard library <thread> for creating threads and std::mutex for protecting shared resources.

Example of simple multithreaded code in C++:

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;

void print_message(const std::string& msg) {
    std::lock_guard<std::mutex> lock(mtx);
    std::cout << msg << std::endl;
}

int main() {
    std::thread t1(print_message, "Hello from thread 1");
    std::thread t2(print_message, "Hello from thread 2");

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

    return 0;
}

This example demonstrates creating two threads that output messages, with access to std::cout protected by a mutex to prevent output mixing.