开发者

Thread communication in unix

i have written a program where the main thread creates a thread and sends an event whenever the secondary thread has to work upon, this works perfectly fine on windows but when porting the same to *nix its difficult to achieve the same.

let me explain, please consider the following

/* thread2 function */
thread_function_call()  
{  
   /* be a consumer */

 pthread_exit();  
}  


/* main function */  
main()  
{  

  /* create a thread */  
  pthread_create();  

  while(1)  
   {  
       /* produce the stuff for consumer or thread2 */  
        /* generate the event */  
   }   

 pthread_join();  
}  

Here the m开发者_Go百科ain thread in a continuous loop and then triggers the event/calls the thread2, once it triggers the event/calls thread2, the thread2 function should execute.

this is something like producer and consumer problem, please do consider that all synchronization conditions are in place.

please help me in how best we can achieve this


The general way to implement a producer-consumer with pthreads is to use a condition variable. The consumer thread(s) block in pthread_cond_wait(), and the producer signals it/them with pthread_cond_signal() or pthread_cond_broadcast().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜