Jump to content

Concurrent computing: Difference between revisions

Removed external library solution, as task can be done through standard ones.
(Updated whitespace, removed versions for old code, and removed code using external libraries as standard libraries allows execution of code. It's suspicious that three standard libraries do the job, so either the task or the code isn't precise enough.)
(Removed external library solution, as task can be done through standard ones.)
Line 120:
 
=={{header|C}}==
{{works under|POSIX}}
{{libheader|pthread}}
<lang c>#include <stdio.h>
#include <unistd.h>
Line 134 ⟶ 132:
} \
pthread_mutex_unlock(&condm); \
} while(0); \
void * t_enjoy(void * p) {
WAITBANG();
Line 164 ⟶ 162:
pthread_join(a[i], NULL);
}
}</lang>
'''Note''': since threads are created one after another, it is likely that the execution of their code follows the order of creation. To make this less evident, I've added the ''bang'' idea using condition: the thread really executes their code once the gun bang is heard. Nonetheless, I still obtain the same order of creation (Enjoy, Rosetta, Code), and maybe it is because of the order locks are acquired. The only way to obtain randomness seems to be to add random wait in each thread (or wait for special cpu load condition)
===OpenMP===
Compile with <code>gcc -std=c99 -fopenmp</code>:
<lang C>#include <stdio.h>
#include <omp.h>
int main() {
const char * str[] = { "Enjoy", "Rosetta", "Code" };
#pragma omp parallel for num_threads(3)
for (int i = 0; i < 3; i++)
printf("%s\n", str[i]);
return 0;
}</lang>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.