Question

I've been trying to implement a dining philosopher solution, using pthreads, i try to run it on my Lenovo yoga2pro pc (runs on windows 8.1) even though i have pthreads for windows installed through mingw installation manager i get this error: it only occurs if there is sleep() method within some part of the code.

C:\Users\Anil\Desktop\Github>gcc dphi.c -pthread
C:\Users\Anil\AppData\Local\Temp\ccAxhsF4.o:dphi.c:(.text+0x185): undefined refe
rence to `sleep'
C:\Users\Anil\AppData\Local\Temp\ccAxhsF4.o:dphi.c:(.text+0x1de): undefined refe
rence to `sleep'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\A
nil\AppData\Local\Temp\ccAxhsF4.o: bad reloc address 0x0 in section `.data'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link
 failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

When i try to compile another file that has no sleep() method in it. this is what i get:

C:\Users\Anil\Desktop\Github>gcc dp.c -pthread
C:\Users\Anil\Desktop\Github>a.exe
Thread 1 says Hello!
Thread 2 says Hi!

It works just fine. i've just started writing this code to learn more about threads because i'm trying to implement ways to solve dining philosopher solution as a project. i don't know what i'm doing wrong tbh. (reason why i'm trying to do this with pthreads on windows is because it's a requirement)

Was it helpful?

Solution

Naive approach:

#include <windows.h>
// Win32's Sleep() is in millis
#define sleep(n) Sleep(n*1000)
#define usleep(n) Sleep(n/1000)

OTHER TIPS

If I'm not wrong, sleep() is part of unistd.h, not pthread ... On linux: add

#include <unistd.h>

(and link the corresponding library if needed)

On windows (your case) I don't know the equivalent, but check you have all the needed includes (and linked libraries)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top