Created
December 30, 2016 17:15
-
-
Save asm-jaime/52ee1ac56ea751df1b23f99cbd939d02 to your computer and use it in GitHub Desktop.
ping pong (threads) c
This file contains hidden or 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
#include <stdio.h> | |
#include <pthread.h> | |
void *ping_task(char *arg) | |
{ | |
printf("ping %s", arg); | |
usleep(1); | |
return (NULL); | |
} | |
void *pong_task(char *arg) | |
{ | |
usleep(1); | |
printf("pong %s", arg); | |
return (NULL); | |
} | |
int main() { | |
pthread_t ping; | |
pthread_t pong; | |
int result; | |
result = pthread_create(&ping, NULL, ping_task, NULL); | |
if ( result != 0 ){ | |
fprintf(stderr, "For some reason I couldn't create a thread! (errno = %d)\n", result); | |
exit(0); | |
} | |
result = pthread_create(&pong, NULL, pong_task, NULL); | |
if ( result != 0 ){ | |
fprintf(stderr, "For some reason I couldn't create a thread! (errno = %d)\n", result); | |
exit(0); | |
} | |
pthread_join( pong, NULL ); | |
pthread_join( ping, NULL ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment