Rendezvous: Difference between revisions
Content added Content deleted
({{omit from|PARI/GP}}) |
(→{{header|C}}: (semi) solution, maybe) |
||
Line 220: | Line 220: | ||
Else |
Else |
||
Return -1 |
Return -1 |
||
}</lang> |
|||
=={{header|C}}== |
|||
Basically just synched threads doing printing: since task didn't ask for service type or resource enumeration, and "message passing is stupid" (c.f. talk), the guarding thread is no more than a glorified mutex, hence completely cut out, leaving the threads directly check ink and do print. |
|||
<lang C>#include <stdio.h> |
|||
#include <unistd.h> |
|||
#include <omp.h> |
|||
typedef struct printer printer; |
|||
struct printer { int id, ink; }; |
|||
printer pnt_main = { 1, 5 }; |
|||
printer pnt_backup = { 2, 5 }; |
|||
int print(char * text, char **error) |
|||
{ |
|||
#pragma omp critical |
|||
{ |
|||
printer *p = &pnt_main; |
|||
if (!p->ink) p = &pnt_backup; |
|||
if (!p->ink) |
|||
*error = "Out of ink"; |
|||
else { |
|||
*error = 0; |
|||
p->ink--; |
|||
printf("%d | ", p->id, p->ink); |
|||
while (*text != '\0') { |
|||
putchar(*(text++)); |
|||
fflush(stdout); |
|||
usleep(30000); |
|||
} |
|||
putchar('\n'); |
|||
} |
|||
} |
|||
return 0 != *error; |
|||
} |
|||
char *humpty[] = { |
|||
"Humpty Dumpty sat on a wall.", |
|||
"Humpty Dumpty had a great fall.", |
|||
"All the king's horses and all the king's men,", |
|||
"Couldn't put Humpty together again." |
|||
}; |
|||
char *goose[] = { |
|||
"Old Mother Goose,", |
|||
"When she wanted to wander,", |
|||
"Would ride through the air,", |
|||
"On a very fine gander.", |
|||
"Jack's mother came in,", |
|||
"And caught the goose soon,", |
|||
"And mounting its back,", |
|||
"Flew up to the moon." |
|||
}; |
|||
int main() |
|||
{ |
|||
int i, j, len; |
|||
char *msg, **text; |
|||
omp_set_num_threads(2); |
|||
#pragma omp parallel for private(text, msg, len, j) |
|||
for (i = 0; i < 2; i++) { |
|||
text = i ? goose : humpty; |
|||
len = (i ? sizeof(goose) : sizeof(humpty) ) / sizeof(char*); |
|||
for (j = 0; j < len; j++) { |
|||
usleep(100000); |
|||
if (print(text[j], &msg)) { |
|||
fprintf(stderr, "Error: %s\n", msg); |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
return 0; |
|||
}</lang> |
}</lang> |
||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
Line 316: | Line 390: | ||
2: And mounting its back, |
2: And mounting its back, |
||
Mother Goose out of ink!</pre> |
Mother Goose out of ink!</pre> |
||
=={{header|Go}}== |
=={{header|Go}}== |