Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdint.h>
- #include <omp.h>
- #include <pthread.h>
- uint64_t inc(uint64_t i) { return ++i; }
- uint64_t _inc(uint64_t i) {
- pthread_t thr; uint64_t ret;
- pthread_create(&thr, NULL, inc, i);
- pthread_join(thr, &ret);
- return ret;
- }
- void inc_test(uint64_t count) {
- uint64_t i = 0;
- while((i = _inc(i)) != count);
- }
- void _run(uint64_t threads, uint64_t count) {
- pthread_t thr[threads];
- uint64_t i = 0;
- do {
- pthread_create(&thr[i], NULL, inc_test, count);
- } while(++i != threads);
- i = 0;
- do {
- pthread_join(thr[i], NULL);
- } while(++i != threads);
- }
- int main(void) {
- uint64_t i = 0, count = 100*1000, threads = 4;
- double start = omp_get_wtime();
- _run(threads, count);
- double time = omp_get_wtime() - start;
- fprintf(stderr, "%lftp/s\n", (count * threads)/time);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment