c - Running two independent threads in parallel -
i have written server program 2 threads:
1. sending data client side. 2. receiving data client side
now when execute them, server accepts request , thread send data created , starts sending data client since joinable, execute until completes execution , other thread not created , control remains in thread.
what need apply in order make both threads run simultaneously?
i think might looking pthread. here sample code wrote when learned how threads work. counter. first thread adds 1 variable each secound, , other prints out.
#include<pthread.h> //for simultanius threads #include<stdio.h> #include<stdlib.h> //for _sleep function //global variables, both thread can reach thease. int = 0; int run = 1; void *fv(void) { while( run ) { _sleep(1000); a++; } } int main(){ pthread_t thread; pthread_create( &thread, 0, fv, 0 ); printf( "%d", ); while( < 60 ) { int last = a; while( last == ) _sleep(50); //let's not burn cpu. printf( "\r%d", ); _sleep(500); //a won't change while } run = 0; pthread_join( thread, null ); return 0; }
Comments
Post a Comment