If you just need some random numbers and don't care if the serial numbers of the random numbers are generated independently or not, you can still use rand() and srand() .
#include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <unistd.h> void* cb1(void*) { while(1) { printf("cb1, rand:%d\n",rand()); sleep(1); } } void* cb2(void*) { while(1) { printf("cb2, rand:%d\n",rand()); sleep(1); } } int main() { pthread_t th1, th2; srand(1); pthread_create(&th1, NULL, cb1, NULL); pthread_create(&th2, NULL, cb2, NULL); pthread_join(th1, NULL); pthread_join(th2, NULL); return 0; }
source share