here is an example I found on internet.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 void *print_message_function( void *ptr );
6
7 main()
8 {
9 pthread_t thread1, thread2;
10 char *message1 = "Thread 1";
11 char *message2 = "Thread 2";
12 int iret1, iret2;
13
14 iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
15 iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
16
17 pthread_join( thread1, NULL);
18 pthread_join( thread2, NULL);
19
20 printf("Thread 1 returns: %d\n",iret1);
21 printf("Thread 2 returns: %d\n",iret2);
22 exit(0);
23 }
24
25 void *print_message_function( void *ptr )
26 {
27 char *message;
28 message = (char *) ptr;
29 for (;;)
30 printf("%s \n", message);
31 }
printf here does not need any lock and I don't know why
printk needs spin_lock or preempt_disable (in my case)
to get a correct output.
regards,
Min-Hua
On 4/25/06, Mulyadi Santosa <mulyadi.santosa@xxxxxxxxx> wrote:
sorry :-)
Hi...
> The kernel is preemptive and context did happen, right?
Yep, it is (at least based on what you wrote on the e-mail)
> why the same code (without spin lock/preempt_disable())
> works fine by pthread API and looks strange when in the kernel.
Please elaborate on what you mean by "works fine by pthread API".... I
really don't understand. Attaching source code is better for
clarification too.
sorry :-)
regards
Mulyadi