0

When I execute the following code in Visual Studio on local windows computer,it runs well.But when I connect it to My Linux in virtual machine,there's something wrong with it.

error info on local windows:The current stack frame was not found in a loaded module. 
Source cannot be shown for this location.

error in Linux Console Window:terminate called after throwing an instance of 'std::system_error'
what():  Enable multithreading to use std::thread: Operation not permitted

Here's the code:

#include<thread>
#include<string>
using namespace std;
void hello(string user_name) {
    cout << "hello," << user_name << "\n";
    cout << std::this_thread::get_id << endl;
}
class scopedThread {
private:
    thread t;
public:
    explicit scopedThread(thread loc) :t(move(loc)) {
        cout << "construct!" << endl;
        if (!t.joinable()) {
            throw logic_error("the thread is unjoinable!");
        }
    }
    ~scopedThread() {
        cout << "deconstruct!" << endl;
        cout << std::this_thread::get_id << "scopedthread1" << endl;
        t.join();
        cout << std::this_thread::get_id << "scopedthread2" << endl;
    }
    scopedThread(scopedThread const&) = delete;
    scopedThread& operator=(scopedThread const&) = delete;
};
int main()
{
    string name;
    cin >> name;
    scopedThread t(thread(hello, name));
    cout << "tian huang bi xia!" << endl;
    return 0;
}

What should I do?

What's the difference between the two compiler?And can I adjust the remote Linux Console in Visual Studio?

Thanks in advance.

p.s.:My compiler on remote Linux is gcc/g++.

Victor Hut
  • 35
  • 5
  • 1
    `std::this_thread::get_id` isn't this missing parentheses? – Borgleader Jul 02 '22 at 02:04
  • 3
    The error says what you need to do. Multithreading isn't enabled. You likely need to pass the `-pthread` option to g++. – Miles Budnek Jul 02 '22 at 02:05
  • @MilesBudnek Then I want to know how can I add something like -pthread to 'Project'->'Options'->'Configuration Properties'->'Debugging'->'Additional Debugger Command' in Visual Studio 2022? – Victor Hut Jul 02 '22 at 02:16
  • 1
    @VictorHut The "Additional Debugger Commands" setting is there for gdb debugger. But the `-lpthread` option that you need to specify is for g++ compiler and linker (to be used while building the executable), not for the gdb debugger (while debugging). – heap underrun Jul 02 '22 at 04:44

0 Answers0