Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <time.h>
- #include <unistd.h>
- #define DATA_SIZE ((int) 100)
- #define NUM_THREADS ((int) 40)
- #define NUM_SWAPS ((int) 2000000)
- int data[DATA_SIZE];
- void *B(void *arg)
- {
- sleep(1);
- int *instancount=(int*)malloc(sizeof(int)*DATA_SIZE);
- for(int i=0;i<DATA_SIZE;i++)
- {
- instancecount[i]=0;
- }
- int *limitptr = (int*)arg;
- int limit=*limitptr;
- for(int i=0;i<limit;i++)
- {
- instancecount[data[i]]++;
- }
- }
- //check that each number from 0 to n occurs exactly once in the array
- pthread_t DataSpawn()
- {
- int i=DATA_SIZE;
- pthread_t tid;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_create(&tid,&attr,B,&i);
- //pthread_join(tid,NULL);
- return tid;
- }
- void swap(int *a, int *b)
- {
- int temp;
- temp = *a;
- *a = *b;
- *b = temp;
- }
- void ZeroInit()// to do: change this name later
- {
- for(int i=0;(i+3)<=DATA_SIZE+2;i++)
- {
- data[i]=i;
- }
- }
- void* A(void *arg)//
- {
- int *limitptr = (int*)arg;
- int limit=*limitptr;
- int idx1=0;
- int idx2=0;
- //srand(time(NULL));
- for(int i=0;i<limit;i++)
- {
- idx1= rand()%NUM_THREADS;
- idx2= rand()%NUM_THREADS;
- swap(&data[idx1],&data[idx2]);
- }
- //return 0;
- pthread_exit(0);
- }
- void SpawnThreads(unsigned int n)
- {
- int iNumThreads = n;
- int ret=-1;
- int ii=NUM_THREADS;
- pthread_t *threadArray=malloc(sizeof(pthread_t)*iNumThreads);
- pthread_t id;
- for(int i=0;i<iNumThreads;i++)
- {
- ret=pthread_create(&threadArray[i],NULL,A,&ii);
- if(ret != 0)
- {
- printf("Pthread error");
- }
- }
- id = DataSpawn();
- for(int i=0;i<iNumThreads;i++)
- {
- pthread_join(threadArray[i],NULL);
- }
- pthread_cancel(id);
- pthread_join(id,NULL);
- }
- int main(int argc, char **argv)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement