Skip to content

Instantly share code, notes, and snippets.

@joelibaceta
Created May 26, 2023 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelibaceta/53e6e1f5ae07821eebbe547ec1c379d4 to your computer and use it in GitHub Desktop.
Save joelibaceta/53e6e1f5ae07821eebbe547ec1c379d4 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#define ITERATIONS 1000000000000
#define UPDATE_INTERVAL 1000000000
#define NUM_THREADS 4
void* process_iterations(void* arg) {
long start = *((long*)arg);
long end = start + ITERATIONS / NUM_THREADS;
for (long i = start; i <= end; ++i) {
if (i % UPDATE_INTERVAL == 0) {
printf("Estado de la iteración: %ld\n", i);
}
}
return NULL;
}
int main() {
pthread_t threads[NUM_THREADS];
long thread_args[NUM_THREADS];
clock_t start_time = clock();
for (int i = 0; i < NUM_THREADS; ++i) {
thread_args[i] = i * (ITERATIONS / NUM_THREADS) + 1;
pthread_create(&threads[i], NULL, process_iterations, (void*)&thread_args[i]);
}
for (int i = 0; i < NUM_THREADS; ++i) {
pthread_join(threads[i], NULL);
}
clock_t end_time = clock();
double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC;
printf("Tiempo transcurrido: %lf segundos\n", elapsed_time);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment