/* An attempt at multithreading using POSIX threads library. */ /* Pretty much useless */ /* Compile with gcc filename.c -lpthread */ #include <stdio.h&rt; #include <pthread.h&rt; int val=0; int exited=0; /* Takes input and displays the value of val. */ void *input_thread(void *ptr) { char ch='d'; while(ch!='q') { scanf("%c",&ch); switch(ch) { case 'd': printf("Val:%d\n",val); break; case 'q': break; } } exited=1; /* Should be kept under lock, but is not. */ } /* Keeps on incrementing val */ void *compute_thread(void *ptr) { while(!exited) val=(val+1)%0xffff; /* Keep it low. This again should have been under lock. */ } int main() { pthread_t i_thread,c_thread; pthread_create(&i_thread,NULL,input_thread,(void *)0); pthread_create(&c_thread,NULL,compute_thread,(void *)0); /* Wait for both threads to exit before exiting main */ pthread_join(i_thread,NULL); pthread_join(c_thread,NULL); return 0; }
Wednesday, November 02, 2005
Multithreading
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment