Home Forums C Programming threads Win32 <-> Linux

Viewing 1 reply thread
  • Author
    • #2098
      Priyansh Agrawal

      I’ve got a problem in porting a multi-thread-c-programm from Win32 to Linux. On both OS there are very similar functions to create threads.
      I use _beginthread in Win32 (process.h) and pthread_create in Linux (pthread.h).

      Normaly both functions work fine in both OS – except for one thing: I have to sync the child-thread with the main-program. So both functions/threads get the same pointer to a status variable which tells them whether to wait or to run.

      In Win32 this works fine. The same code in Linux produces an endless loop.
      The child-thread has something like: while(*status == 0);
      “status” is a pointer to an integer.
      I know, normally this IS an endless loop. But NOT when the other process changes the value of this variable.

      The pointers in both processes point exactly to the same address. In Win32 this loop recognizes when the value has changed. But not in Linux ???!!!
      I don’t understand this behaviour at all.
      Ahh, I found out something weird:
      When the program produces output on the console, the Linux-code works, too.
      But I don’t want to produce console-output in this loop!

      Could anybody help?

    • #3387
      Priyansh Agrawal

      Ok, I found the problem/solution but I’m not satisfied: All works fine when the optimizations in gcc are turned off.
      Each optimization (O, O1, O2, O3, Os) will cause an endless loop.

      Here an example:

Viewing 1 reply thread
  • The forum ‘C Programming’ is closed to new topics and replies.