threads Win32 <-> Linux

Home Forums C Programming threads Win32 <-> Linux

This topic contains 1 reply, has 1 voice, and was last updated by  Priyansh Agrawal 9 years, 7 months ago.

  • Author
    Posts
  • #2098

    Hi,
    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

    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:

You must be logged in to reply to this topic.

0 Shares
Share
+1
Tweet
Share
Stumble