thread in class c++ code example
Example 1: thread c++
#include <iostream>
#include <thread>
void foo()
{
}
void bar(int x)
{
}
int main()
{
std::thread first (foo);
std::thread second (bar,0);
std::cout << "main, foo and bar now execute concurrently...\n";
first.join();
second.join();
std::cout << "foo and bar completed.\n";
return 0;
}
Example 2: c++ create threads
#include <thread>
void foo()
{
}
int main()
{
std::thread first (foo);
first.join();
}
Example 3: c++ thread incide class
#include <iostream>
#include <thread>
class foo
{
public:
void make_foo_func_thread()
{
t=std::thread(&foo::foo_func, this);
t.join();
}
private:
std::thread t;
void foo_func() { std::cout << "Hello\n"; }
};
int main()
{
foo f;
f.make_foo_func_thread();
}