very basic but nonunderstandable

Thanks everybody who will respond in advance ...

after executing the code below I expect to get on the output:
9
9
but instead I get:
4
9

#include <stdio.h>
#include <pthread.h>
int a;
pthread_t thread_id;
pthread_mutex_t mutex1=PTHREAD_MUTEX_INITIALIZER;
void *thread_function(void *unused)
{
pthread_mutex_lock(&mutex1);
a=9;
sleep(3);
printf("%i\n",a);
pthread_mutex_unlock(&mutex1);
return NULL;
}
int main()
{
pthread_create(&thread_id,NULL,&thread_function,NULL);
a=4;
printf("%i\n",a);
pthread_join(thread_id,NULL);
return 0;
}
[673 byte] By [ventsi16] at [2007-11-18 20:30:48]
# 1 Re: very basic but nonunderstandable
The operating system simply needs some time to actually create the thread...while it is being created your processing of the 'main()' function still continues, thus, printing the '4' before the '9'...
Andreas Masur at 2007-11-9 13:56:12 >
# 2 Re: very basic but nonunderstandable
Thank you ,it sounds convincingly.
ventsi16 at 2007-11-9 13:57:12 >
# 3 Re: very basic but nonunderstandable
You are welcome...
Andreas Masur at 2007-11-9 13:58:10 >