赞
踩
#include <thread>
std::thread thObj(<CALLBACK>);
#include <thread>
void thread_function() {
for(int i = 0; i < 10000; i++);
std::cout<<"thread function Executing"<<std::endl;
}
int main() {
std::thread threadObj(thread_function);
for(int i = 0; i < 10000; i++);
std::cout<<"Display From MainThread"<<std::endl;
threadObj.join();
std::cout<<"Exit of Main function"<<std::endl;
return 0;
}
#include <iostream>
#include <thread>
class DisplayThread {
public:
void operator()() {
for(int i = 0; i < 10000; i++)
std::cout<<"Display Thread Executing"<<std::endl;
}
};
int main() {
std::thread threadObj( (DisplayThread()) );
for(int i = 0; i < 10000; i++)
std::cout<<"Display From Main Thread "<<std::endl;
std::cout<<"Waiting For Thread to complete"<<std::endl;
threadObj.join();
std::cout<<"Exiting from Main Thread"<<std::endl;
return 0;
}
#include <iostream>
#include <thread>
int main() {
int x = 9;
std::thread threadObj([]{
for(int i = 0; i < 10000; i++)
std::cout<<"Display Thread Executing"<<std::endl;
});
for(int i = 0; i < 10000; i++)
std::cout<<"Display From Main Thread"<<std::endl;
threadObj.join();
std::cout<<"Exiting from Main Thread"<<std::endl;
return 0;
}
std::thread::get_id()
std::this_thread::get_id()
#include <iostream>
#include <thread>
void thread_function() {
std::cout<<"Inside Thread :: ID = "<<std::this_thread::get_id()<<std::endl;
}
int main() {
std::thread threadObj1(thread_function);
std::thread threadObj2(thread_function);
if(threadObj1.get_id() != threadObj2.get_id())
std::cout<<"Both Threads have different IDs"<<std::endl;
std::cout<<"From Main Thread :: ID of Thread 1 = "<<threadObj1.get_id()<<std::endl;
std::cout<<"From Main Thread :: ID of Thread 2 = "<<threadObj2.get_id()<<std::endl;
threadObj1.join();
threadObj2.join();
return 0;
}
<future>
中包含。#include <iostream>
#include <future>
#include <thread>
using namespace std;
int main() {
promise<int> a_promise;
auto a_future = a_promise.get_future();
a_promise.set_value(10);
cout << a_future.get() << endl;
cout << "after get()" << endl;
return 0;
}
10
after get()
#include <iostream>
#include <future>
using namespace std;
int f() {
string hi = "hello, world!";
cout << hi << endl;
return hi.size();
}
int main() {
packaged_task<int ()> task(f);
auto result = task.get_future();
task();
cout << result.get() << endl;
return 0;
}
hello, world!
13
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <future>
template <typename RandomIt>
int parallel_sum(RandomIt beg, RandomIt end) {
auto len = end - beg;
if (len < 1000)
return std::accumulate(beg, end, 0);
RandomIt mid = beg + len/2;
auto handle = std::async(std::launch::async,
parallel_sum<RandomIt>, mid, end);
int sum = parallel_sum(beg, mid);
return sum + handle.get();
}
int main() {
std::vector<int> v(10000, 1);
std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
}
The sum is 10000
#include <iostream>
#include <future>
#include <thread>
using namespace std;
int f(promise<int> my_promise) {
string hi = "hello, world!";
my_promise.set_value(hi.size());
this_thread::sleep_for(0.1s);
cout << hi << endl;
}
int main() {
promise<int> f_promise;
auto result = f_promise.get_future();
thread f_thread(f, move(f_promise));
cout << result.get() << endl;
f_thread.join();
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。