Can I put thread inside thread in C++?

Asked 2 years ago, Updated 2 years ago, 48 views

Can I put a thread inside the thread?

If you put a thread inside the thread now, Python can put a thread inside the thread (if you say a thread inside the thread A is B)

AB / AB / AB / AB... It repeats like this (both A and B threads have an infinite loop of while True)

But I put thread inside thread for C++

ABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB... It's printed like this. In other words, the first thread is only once, and only the thread in the thread is repeated indefinitely.

Like Python, AB/AB/AB/AB... Can't I repeat it like this?

Even if the thread inside the thread is detected, AAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB... It came out like this.

The best case is to have A and B go around the infinite loop. The problem is that the thread cannot be turned directly from the main.

The reason why I put B in A now is that the information received from other clients from A enters the forwarding factor and B's thread must be executed. (A is the server code that can always recv.)

c++

2022-09-21 10:24

1 Answers

There is no concept of thread-in-thread unless you are in a special situation, such as fiber.

It is thought to mean that there is a handle of another thread in a variable in the stack, as shown below. This is not called thread-in-thread.

void funcB() {
    while (true){
        // ...
    }
}
void funcA() {
    std::thread b(funcB);
    b.detach();
    while (true){
        // ...
    }
}
int main() {
    std::thread a(funcA);
    a.join();
    return 0;
}

ABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB... It's printed like this. In other words, the first thread is only once, and only the thread in the thread is repeated indefinitely.

Threads for all applications running in the OS are scheduled by the OS. The OS determines which threads are allocated and how much time is spent on which core. This is the same for both C++ and Python.

If it works like ABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB, this is because the OS has not yet decided that thread A should be run. The reason why OS made such a judgment is that the scheduling of threads is not based on a single while loop.

The traditional method is to run each thread in a certain time quantum. If thread A and B are allocated time to use CPU 1ms each because this time unit is 1ms, and it takes 0.0001ms to turn the while loop once, thread B will turn the loop 1000 times and thread A will operate.

Unless the two threads are in a competitive state, one thread will not operate unilaterally.

Like Python, AB/AB/AB/AB... Can't I repeat it like this?

thread to the roof standards and action is not to go back to once is os "The other motion and ordered it for the thread" , we need to let me know.

This means that the OS has used up its CPU, so it will give way to other threads. When this function is invoked, the OS stops executing the current thread and executes another thread, although it is not yet time to switch to another thread.

However, calling yard() does not necessarily mean that the OS follows it, so it does not operate in the order of AB/AB/AB.

condition_variable is used to synchronize flows between threads. One of the features it provides is notification.

This can be used with the code below, but since the two threads take turns, one thread waits for the other to complete.

std::mutex mutex;
std::condition_variable cv;
bool flag = true;
void funcB() {
    while (true) {
        std::unique_lock<std::mutex> lock(mutex);
        cv.wait(lock, []() { return !flag; });
        // ...
        flag = true;
        lock.unlock();
        cv.notify_one();
    }
}
void funcA() {
    std::thread b(funcB);
    b.detach();
    while (true){
        std::unique_lock<std::mutex> lock(mutex);
        cv.wait(lock, []() { return flag; });
        // ...
        flag = false;
        lock.unlock();
        cv.notify_one();
    }
}
int main() {
    std::thread a(funcA);
    a.join();
    return 0;
}

In addition, in Python, AB/AB/AB/AB... To guess if it worked, I think it might be because of GIL.


2022-09-21 10:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.