Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdlib.h>
- #include <stdio.h>
- #define M (4*500000000)
- #define N 4
- static volatile int count = 0;
- static int *res;
- static int do_calc (int x)
- {
- int res;
- res = x ^ (x >> 16);
- res *= 0xcafebabe;
- res = res ^ (x >> 8);
- res *= 0xfeedabba;
- return res;
- }
- void* start (void *arg)
- {
- int start, len, i;
- start = __sync_add_and_fetch (&count, 1);
- len = M / N;
- start = (start-1)*len;
- for (i = start; i < start+len; i++) res[i] = do_calc(i);
- return NULL;
- }
- int main ()
- {
- int i;
- pthread_t threads[N];
- res = malloc (sizeof(int)*M);
- //start(NULL);
- for (i = 0; i < N; i++)
- pthread_create (&(threads[i]), NULL, start, NULL);
- for (i = 0; i < N; i++)
- pthread_join (threads[i], NULL);
- printf ("%i\n", count);
- /* for (i=0; i<M; i++) */
- /* if (do_calc(i) != res[i]) printf ("%i %i\n", do_calc(i), res[i]); */
- free (res);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment