As mentioned earlier, there are many drawbacks to using non-blocking methods. Mainly single-threaded, has been occupying CPU resources, other programs can not be executed, resulting in waste of resources. Can only be used for sites with simpler loops. While the thread is flexible and the CPU usage is small, it is suitable for most occasions.
1. Use of condition variables
A condition variable is a mechanism used to notify state information about shared data. Because of the shared data involved, it is used in conjunction with the mutex.
1.1 Creating and destroying condition variables
pthread_cond_t represents a condition variable. Initializes the pthread_cond_t variable before it is used. With the default properties, you can use Pthread_cond_initializer to pay variables to complete the initialization.
Pthread_cond_desroy destroys a condition variable. function in the form of
1 Pthread_cond_desroy (pthread_cond_t * cond)
1.2 Wait and notify condition variables
The condition variables are called together with the condition test. If the test fails, call pthread_cond_wait
int pthread_cond_wait (pthread_cond_t *restrict cond,pthread_mutex_t *restrict Mutex)
If the test succeeds, call the Pthread_cond_signal function.
1 int pthread_cond_signal (pthread_cond_t *cond)
The pthread_cond_signal function wakes only one thread that is blocking the condition variable that the cond points to.
2 Application of conditional variables
2.1 Creating a thread within the Allinputdevicesinit function
1 if (0 = =pttmp-> deviceinit ())2{3 */** 4 pthread_create (&pttmp->ttreadid, NULL, inputeventtreadfunction, pttmp-> getinputevent); 5 0 ; 6 }7 pttmp = pttmp->ptnext;
Implementing the Inputeventtreadfunction function
1 Static void*inputeventtreadfunction (void*pVoid)2 {3 t_inputevent tinputevent;4 5 /*Defining function Pointers*/6 int(*getinputevent) (Pt_inputevent ptinputevent);7Getinputevent = (int(*) (pt_inputevent)) pVoid;8 9 while(1)Ten { One if(0= = Getinputevent (&tinputevent)) A { - /*wakes the main thread, assigns the value of the tinputevent to a global variable*/ - /*obtain the mutex before accessing the critical resource*/ thePthread_mutex_lock (&G_tmutex); -G_tinputevent =tinputevent; - - /*Wake main thread*/ +Pthread_cond_signal (&G_tconvar); - + /*Release Mutex amount*/ APthread_mutex_unlock (&G_tmutex); at } - } - - returnNULL; -}
2.2 Change to blocking mode in TOUCHSCREEN.C
The Touchscreengetinputevent function is modified as follows:
1 Static inttouchscreengetinputevent (pt_inputevent ptinputevent)2 {3 structts_sample Tsamp;4 structts_sample tsamppressed;5 structts_sample tsampreleased;6 intIRet;7 intbstart =0;8 intIdelta;9 Ten Static structtimeval tpretime; One A - while(1) - { theIRet = Ts_read (G_ttsdev, &tsamp,1);/*If countless data are dormant*/ - if(IRet = =1) - { - if((Tsamp.pressure >0) && (bstart = =0)) + { - /*just pressed the*/ + /*record the point at which it was initially pressed*/ Atsamppressed =Tsamp; atbstart =1; - } - - if(Tsamp.pressure <=0) - { - /*Loosen*/ intsampreleased =Tsamp; - to /*working with Data*/ + if(!bstart) - { the return-1; * } $ ElsePanax Notoginseng { -Idelta = tsampreleased.x-tsamppressed.x; thePtinputevent->ttime =tsampreleased.tv; +Ptinputevent->itype =Input_type_touchscreen; A the if(Idelta > gixres/5) + { - /*turn to the previous page*/ $Ptinputevent->ival =input_value_up; $ } - Else if(Idelta <0-gixres/5) - { the /*turn to the next page*/ -Ptinputevent->ival =Input_value_down;Wuyi } the Else - { WuPtinputevent->ival =Input_value_unknown; - } About return 0; $ } - } - } - Else A { + return-1; the } - } $ the return 0; the}
The main realization of the above program: Touch Point in the X-direction of the difference in the value of the X resolution of 1/5, the page, so as to achieve sliding page.
2. Get standard input using threading method