I’ve tried to execute first example from Linux Tutorial Posix Threads.
That’s what I have:
[alex@Allok c_c++]$ g++ -lpthread from.cpp
from.cpp: In function ‘int main()’:
from.cpp:10:22: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
from.cpp:11:22: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
[alex@Allok c_c++]$ ./a.out
Thread 2
Thread 1
Thread 1 returns: 0
Thread 2 returns: 0
The problem is that I expected to have the output like the source says:
Thread 1
Thread 2
Thread 1 returns: 0
Thread 2 returns: 0
I don’t understand why it happens. Can anyone help me?
[alex@Allok c_c++]$ uname -a
Linux Allok 3.3.2-1-ARCH #1 SMP PREEMPT Sat Apr 14 10:08:43 UTC 2012 i686 AMD Athlon(tm) II Neo K125 Processor AuthenticAMD GNU/Linux
Threads, by definition, are executed asynchronously (it’s all at the whim of the OS scheduler). You can’t assume anything about how they execute with respect to one another, unless you explicitly use synchronisation mechanisms.