Created
February 11, 2021 21:56
-
-
Save arthur-mts/116772ef34b267b31f3ed8eed97a2202 to your computer and use it in GitHub Desktop.
Modification of threaddemo.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* threaddemo.c */ | |
/* Thread demonstration program. Note that this program uses a shared variable | |
in an unsafe manner (eg mutual exclusion is not attempted!) */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
const clock_t MAXDELAY = 2000000; | |
const char *FIRST = "first thread"; | |
const char *SECOND = "second thread"; | |
void delay(clock_t ticks) { /* a "busy" delay */ | |
clock_t start = clock(); | |
do | |
; while (clock() < start + ticks); | |
} | |
void * adjustX(void *n) | |
{ | |
int i = (int)n; | |
int x = 50; | |
while (1) /* loop forever */ | |
{ | |
printf("%s: %i\n", i == 1 ? FIRST : SECOND, x); | |
x += i; | |
delay(rand()%MAXDELAY); | |
} | |
return(n); | |
} | |
main() | |
{ int a; | |
srand(time(NULL)); | |
pthread_t up_thread, dn_thread; | |
pthread_attr_t *attr; /* thread attribute variable */ | |
attr=0; | |
printf("creating threads:\n"); | |
pthread_create(&up_thread,attr, adjustX, (void *)1); | |
pthread_create(&dn_thread,attr, adjustX, (void *)-1); | |
while (1) /* loop forever */ | |
{ ;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment