Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct
- {
- double speed_in_kmh;
- double current_power_in_mega_watts;
- } DeLorean;
- typedef struct
- {
- unsigned int* cap_indices;
- unsigned int array_length;
- } IndexContainer;
- typedef struct {
- unsigned char* c_string;
- unsigned int value;
- } FluxCapacitor;
- DeLorean* delorean__;
- FluxCapacitor** capacitors__;
- pthread_mutex_t lock;
- // Thread function.
- void* assembleDeLorean(void* indices)
- {
- // Get mutex to lock function and make sure
- // that only one thread at a time is using it.
- pthread_mutex_lock(&lock);
- // Declare "indices" parameter as IndexContainer.
- IndexContainer* iC = indices;
- double sum = 0;
- // Iterate through all given indices in iC
- // and add value of iC to sum, if index exists.
- for (int i = 0; i < 121; ++i)
- {
- for (int j = 0; j < iC->array_length; ++j)
- {
- if (i == iC->cap_indices[j])
- {
- sum += capacitors__[i]->value;
- break;
- }
- }
- }
- // Assign computed sum to power of global delorean.
- delorean__->current_power_in_mega_watts = sum;
- // Release mutex.
- pthread_mutex_unlock(&lock);
- // Stop thread.
- pthread_exit(NULL);
- }
- int main(void)
- {
- printf("Main thread "main()" was started.n");
- capacitors__ = createFluxCapacitorArray(121);
- delorean__ = createDeLorean(0, 0);
- IndexContainer* iC_1 = malloc(sizeof(*iC_1));
- iC_1->array_length = 21;
- iC_1->cap_indices = malloc(21 * sizeof(unsigned int));
- IndexContainer* iC_2 = malloc(sizeof(*iC_2));
- iC_2->array_length = 100;
- iC_2->cap_indices = malloc(100 * sizeof(unsigned int));
- // Fill iC_1.
- for (int i = 0; i < 21; ++i)
- {
- iC_1->cap_indices[i] = i;
- }
- // Fill iC_2.
- int k = 0;
- for (int i = 21; i < 121; ++i)
- {
- iC_2->cap_indices[k] = i;
- ++k;
- }
- // Declare threads.
- pthread_t thread1, thread2;
- int rT1, rT2;
- // Initialize mutex protecting "assembleDeLorean" function.
- pthread_mutex_init(&lock, NULL);
- // Create & run first thread.
- printf("Creating and running thread1.n");
- rT1 = pthread_create(&thread1, NULL, assembleDeLorean, &iC_1);
- if (rT1 != 0)
- {
- printf("Thread 1 could not be created.n");
- return EXIT_FAILURE;
- }
- printf("Return value of creation of thread1: %dn", rT1);
- // Create & run second thread.
- printf("Creating and running thread2.n");
- rT2 = pthread_create(&thread2, NULL, assembleDeLorean, &iC_2);
- if (rT2 != 0)
- {
- printf("Thread 2 could not be created.n");
- return EXIT_FAILURE;
- }
- printf("Return value of creation of thread2: %dn", rT2);
- // Wait for threads to finish.
- printf("Waiting for thread1 to finish...n");
- if (pthread_join(thread1, NULL))
- {
- printf("An error occured while joining thread1.n");
- return EXIT_FAILURE;
- }
- printf("Thread1 finished!");
- printf("Waiting for thread2 to finish...n");
- if (pthread_join(thread2, NULL))
- {
- printf("An error occured while joining thread2.n");
- return EXIT_FAILURE;
- }
- printf("Thread2 finished!");
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment