Skip to content

Instantly share code, notes, and snippets.

@morsoinferno
Created May 3, 2017 15:06
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 morsoinferno/4c39ef611baa41288670b3fb3b576cbd to your computer and use it in GitHub Desktop.
Save morsoinferno/4c39ef611baa41288670b3fb3b576cbd to your computer and use it in GitHub Desktop.
codigo_control_3.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX_THREADS 5
int i, sum;
enum {THREAD_FAIL, THREAD_SUCCESS};
void *runner(void *param){
int limit = atoi(param);
int i, check;
sum=0; check=0;
if (limit > 0){
for (i = 1; i <= limit; i++){
sum += i;
check += i;
}
}
if (check == sum) pthread_exit((void *) THREAD_SUCCESS);
else pthread_exit((void *) THREAD_FAIL);
}
char* statusToString (int status){
return status == THREAD_SUCCESS ? "success!":"fail";
}
int main(int argc, char *argv[]) {
pthread_t threads[MAX_THREADS];
int status;
for (i=0;i<MAX_THREADS;i++)
pthread_create(&threads[i], NULL, (void*)runner, (void *) argv[1]);
for (i=0;i<MAX_THREADS;i++){
pthread_join(threads[i], (void **)&status);
printf("sum = %d, status = %s\n", sum, statusToString(status));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment