jeudi 23 mai 2019

Get errors with rand and rand_r when multithreading

I am creating many threads and each one should output a random number.

I know that srand() with rand is not thread-safe and indeed all the output numbers are the same.

So I tried to use rand_r but I get the following error on my Windows terminal

main.c:47:16: warning: implicit declaration of function 'rand_r'; did you mean 'rand'? [-Wimplicit-function-declaration]
 result= ( (rand_r(&seed) % (high+1-low) ) + low);
            ^~~~~~
            rand
main.c: In function 'customerServe':
main.c:333:1: warning: control reaches end of non-void function [-Wreturn-type]
}

c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: 
C:\Users\allys\AppData\Local\Temp\ccSslADA.o:main.c:(.text+0xe): undefined 
reference to `rand_r'
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: 
C:\Users\allys\AppData\Local\Temp\ccSslADA.o:main.c:(.text+0x41c): 
undefined reference to `rand_r'
collect2.exe: error: ld returned 1 exit status

Thank you




Aucun commentaire:

Enregistrer un commentaire