Jump to content

Concurrent computing: Difference between revisions

m
Removed output as specified in task and updated whitespace.
m (Updated whitespace.)
m (Removed output as specified in task and updated whitespace.)
Line 237:
{{works with|C++11}}
The following example compiles with GCC 4.7.
 
<code>g++ -std=c++11 -D_GLIBCXX_USE_NANOSLEEP -o concomp concomp.cpp</code>
 
<lang cpp>#include <thread>
#include <iostream>
Line 245 ⟶ 243:
#include <random>
#include <chrono>
int main() {
 
std::random_device rd;
int main()
std::mt19937 eng(rd()); // mt19937 generator with a hardware random seed.
{
std::random_devicemt19937 eng(rd());
std::uniform_int_distribution<> dist(1,1000);
std::mt19937 eng(rd()); // mt19937 generator with a hardware random seed.
std::vector<std::thread> threads;
std::uniform_int_distribution<> dist(1,1000);
for (const auto& str: {"Enjoy\n", "Rosetta\n", "Code\n"}) {
std::vector<std::thread> threads;
// between 1 and 1000ms per our distribution
 
std::chrono::milliseconds duration(dist(eng));
for(const auto& str: {"Enjoy\n", "Rosetta\n", "Code\n"}) {
threads.emplace_back([str, duration]() {
// between 1 and 1000ms per our distribution
std::chronothis_thread::milliseconds durationsleep_for(dist(eng)duration);
std::cout << str;
 
});
threads.emplace_back([str, duration](){
}
std::this_thread::sleep_for(duration);
for(auto& t: threads) t.join();
std::cout << str;
} t.join();
}
 
for(auto& t: threads) t.join();
 
return 0;
}</lang>
 
Output:
<pre>Enjoy
Code
Rosetta</pre>
 
{{libheader|Microsoft Parallel Patterns Library (PPL)}}
 
<lang cpp>#include <iostream>
#include <ppl.h> // MSVC++
#include <ppl.h>
 
void a(void) { std::cout << "Eat\n"; }
void b(void) { std::cout << "AtEat\n"; }
}
void c(void) { std::cout << "Joe's\n"; }
void b(void) {
 
std::cout << "At\n";
int main()
}
{
void c(void) {
void c(void) { std::cout << "Joe's\n"; }
}
int main() {
// function pointers
Concurrency::parallel_invoke(&a, &b, &c);
Line 295 ⟶ 286:
return 0;
}</lang>
Output:
<pre>
Joe's
Eat
At
Enjoy
Code
Rosetta
</pre>
 
=={{header|Cind}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.