0

What i tried to do:

I tried to make program witch goal is add elements to the queue (in thread) and display data about queue (You can see data to display in main). Before that i wanted to delete one element form queue (every two seconds) and adding new element (every one second).

Code

#include <iostream>
#include <queue>
#include <thread>
#include <Windows.h>

using std::queue;
using std::cout;

void loadQueue(queue<int> &toLoad)
{
    for(int i = 0; i < 100; i++)
    {
        toLoad.push(i); 
        Sleep(1000);
    }
}

int main(void)
{
    queue<int>toLoad;
    std::thread(loadQueue, std::ref(toLoad));

    while(true)
    {
        cout << "SIZE OF QUEUE : " << toLoad.size() << '\n' << '\n'; 

        cout <<"FRONT :" << toLoad.front() << '\n' << '\n';

        cout <<"BACK : " << toLoad.back() << '\n';
        toLoad.pop();
        Sleep(2000);
    }

}

About error

When I start program i don't see enything. Program close right away. Visual Studio show me this messege:

ERROR SHOWING BY VISUAL STUDIO CODE AFTER START PROGRAM

5
  • 4
    std::thread(loadQueue, std::ref(toLoad)); — this declares a local temporary variable of type std::thread. Since it is unnamed, temporary object, it gets destructed immediately after its creation. But see the destructor of std::thread — quote: "If *this has an associated thread (joinable() == true), std::terminate() is called. " You need to join the threads you create. (Or detach, but this is advanced usage.) Commented Apr 10, 2023 at 19:54
  • 2
    Sleep(1000); --> #include <chrono> ... std::this_thread::sleep_for(1000ms);. In other words, use the standard thread functions, not Windows specific ones. Commented Apr 10, 2023 at 20:17
  • 1
    Take the message's advice: Press Retry and debug the program. It's sickeningly effective. Commented Apr 10, 2023 at 21:22
  • There is clearly a data race in the code. Several can access same data only if none try to modify it. Here both modify the deque, that results as an Undefined Behavior. All accesses to 'toLoad` must be done in a mutex protected area.
    – dalfaB
    Commented Apr 11, 2023 at 17:05
  • meta.stackoverflow.com/a/285557/8400969 Commented Apr 13, 2023 at 18:12

1 Answer 1

7

std::thread cannot be destroyed while it's attached to an execution thread. In your case, however, you introduce a temporary in this line of code which gets destroyed the moment the expression finishes:

std::thread(loadQueue, std::ref(toLoad));

You either need to detach the thread and let it be:

std::thread{ loadQueue, std::ref(toLoad) }.detach();

or name the variable and keep it alive while your work is happening:

std::thread thread{ loadQueue, std::ref(toLoad) };

Since you have infinite loop in the main thread, this thread will never be destroyed, but ideally you want to join it somewhere, e.g. at the end of the main function:

while(true)
{
   ... 
}
thread.join();

Also be advised, that std::queue is not a thread-safe class, so you have to manually synchronise access to it, otherwise you have race condition in you code which is UB by the standard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.