简体   繁体   中英

Multithreading error in Ubuntu Virtual Machine?

I am a beginner in multithreading and I use Ubuntu 16.04.2 LTS Virtual Machine from Windows 10. I started multithreading by compiling a very simple hello world file:

#include <iostream>
#include <thread>

using namespace std;

void call_from_thread()
{
    cout << "Hello Ubuntu" << endl;
}

main()
{
    cout << "Hello Ubuntu from main" << endl;
    pthread t1(call_from_thread);
    t1.join();
}

When I try to compile this using the following command:

g++ -std=c++11 -o multithread test2.cpp -pthread

it throws the following error:

'pthread' was not declared in this scope.

In the line pthread t1(call_from_thread); What am I missing here? Does it need any additional libraries to be downloaded?

To use pthread you need to pthread.h #include<pthread.h>

'thread' and 'pthread.h' are different, though 'thread' is made using pthread.h

So, in the above case, you just need to inlclude <pthread.h> library, other than that everything is all right.

And If you are interested in using thread (std::thread), then check out how to use std::thread here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM