Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <iostream>
- #include <sys/time.h>
- #include <sys/param.h>
- #include <pthread.h>
- #define TYPE int
- using namespace std;
- const int count_Thread = 2;
- struct thread_argument
- {
- int id; // user identification
- int from, length; // data range
- TYPE *data;
- };
- int rand32()
- {
- return rand() * RAND_MAX + rand();
- }
- void fill_array(TYPE *arr, int start, int end)
- {
- for (int i = start; i < end; i++)
- {
- arr[i] = rand32() % (end * 10);
- }
- }
- void *fill_thread( void *void_arg )
- {
- thread_argument *ptr_data = ( thread_argument * ) void_arg;
- printf( "Thread %d started from %d with length %d...\n",
- ptr_data->id, ptr_data->from, ptr_data->length );
- fill_array( ptr_data->data, ptr_data->from, ptr_data->length );
- return NULL;
- }
- int main( int na, char **arg )
- { int my_length;
- cin>>my_length;
- TYPE *my_array = new TYPE [ my_length ];
- if ( !my_array )
- {
- printf( "Not enought memory for array!\n" );
- return 1;
- }
- pthread_t pt[count_Thread];
- thread_argument ta[count_Thread];
- // Initialization of thread arguments
- for (int i = 0; i < count_Thread; i++)
- {
- ta[i].id = i;
- ta[i].from = i * (my_length/count_Thread);
- ta[i].to = ta[i].from + (my_length/count_Thread);
- ta[i].data = my_array;
- }
- for (int i = 0; i < count_Thread; i++)
- {
- pthread_create( &pt[i], NULL, fill_thread, &ta[i] );
- }
- for (int i = 0; i < count_Thread; i++)
- {
- pthread_join( pt[i], NULL );
- }
- for(int i = 0; i < my_length;i++)
- {
- cout>>(my_array[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement