Skip to content

Instantly share code, notes, and snippets.

@depp
Created April 30, 2015 21:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save depp/3f04508a88a114734195 to your computer and use it in GitHub Desktop.
Save depp/3f04508a88a114734195 to your computer and use it in GitHub Desktop.
Notice how each number appears only once.
The order is still scrambled, but we don't care about the order.
Arg = 1
Arg = 4
Arg = 2
Arg = 6
Arg = 3
Arg = 7
Arg = 0
Arg = 5
Arg = 8
Arg = 9
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#define N 10
void *thread_func(void *arg)
{
int val = (int) (intptr_t) arg;
printf("Arg = %d\n", val);
return NULL;
}
int main()
{
int i;
pthread_t threads[N];
for (i = 0; i < N; i++) {
pthread_create(&threads[i], NULL, thread_func,
(void *) (intptr_t) i);
}
for (i = 0; i < N; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment