Skip to content

Instantly share code, notes, and snippets.

@asm-jaime
Created December 30, 2016 17:15
Show Gist options
  • Save asm-jaime/52ee1ac56ea751df1b23f99cbd939d02 to your computer and use it in GitHub Desktop.
Save asm-jaime/52ee1ac56ea751df1b23f99cbd939d02 to your computer and use it in GitHub Desktop.
ping pong (threads) c
#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