Skip to content

Instantly share code, notes, and snippets.

@atimin
Created August 7, 2009 14:32
Show Gist options
  • Save atimin/163926 to your computer and use it in GitHub Desktop.
Save atimin/163926 to your computer and use it in GitHub Desktop.
Work with pthread in C
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *start_routine(void *arg)
{
int *i = (int *)arg;
while(*i< 10) {
sleep(1);
*i += 1;
printf("i = %i\n", *i);
}
pthread_exit(0);
}
int main()
{
pthread_t tid;
int ret;
int *result = malloc(sizeof(int));
*result = 0;
ret = pthread_create(&tid, NULL, start_routine, (void *)result);
//pthread_join(tid, NULL);
while(*result < 10) {
sleep(1);
printf("result = %i\n", *result);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment