Pthread_setschedparam(thread1, SCHED_FIFO, ¶m) Pthread_create(&thread5, NULL, thread5Go, NULL) // medium priority: navigation Pthread_setschedparam(thread4, SCHED_FIFO, ¶m) Pthread_create(&thread4, NULL, thread4Go, NULL) // medium priority: UART comm <<< test !! Pthread_setschedparam(thread3, SCHED_FIFO, ¶m) Pthread_create(&thread3, NULL, thread3Go, NULL) // highest priority: encoder reading Pthread_setschedparam(thread2, SCHED_RR, ¶m) Pthread_create(&thread2, NULL, thread2Go, NULL) // medium priority: motor control Pthread_setschedparam(thread1, SCHED_RR, ¶m) Pthread_create(&thread1, NULL, thread1Go, NULL) // low priority: keyboard monitoring (stop program) Pthread_setschedparam(thread0, SCHED_RR, ¶m) Pthread_create(&thread0, NULL, thread0Go, NULL) // lowest priority task: screen output Code: Select all pthread_t thread0, thread1, thread2, thread3, thread4, thread5 Pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL) Ĭode compilation: gcc pthread_test. When the thread receives the Cancel signal, it exits immediately.*/ *Synchronized Cancel, Cancel at Next Cancel Point*/ // pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) /*Asynchronous Cancel. Sample code: #include #include #include void *thread_fun( void *arg)
JOIN AND CANCEL PTHREAD C CODE
Printf( "thread 3 exit code %d\n", ( int)ret) Pthread_create(&tid, NULL,thread_fun, NULL) *Manually create a cancel point to prevent the thread from having no cancel point, resulting in the thread not canceling.*/ Pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) *Synchronized Cancel, Cancel at Next Cancel Point*/ Pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) Value_ptr: The return value of the exit thread.Ĭode example: #include #include #include void *thread_fun( void *arg) Thread: The thread number waiting to exit the thread. int pthread_join(pthread_t thread, void **value_ptr) Test exit point is to test cancel signal.Į. In addition to calling programmatically established cancellation points by pthread_testcancel(), the pthread standard specifies several cancellation points. If pthread_testcancel() is called when the cancel function is disabled, the function does not work.īe sure to insert pthread_testcancel() only in the sequence of thread cancellation thread operation security. The pthread_testcancel() function is valid when the thread cancel function is enabled and the cancel state is set to a delayed state. This means that pthread_testcancel does not contain cancellation points, but it needs to create a cancellation point where cancellation points are not included to respond to cancellation requests in an execution code thread that does not contain cancellation points. Setting the execution time of this thread's cancel action, the type is selected by two values: PTHREAD_CANCEL_DEFFERED and PTHRAD_CANCEL_ASYCHRONOUS, which are valid only when the Cancel state is Enable, indicating that the cancel action will continue to run to the next cancel point after receiving the signal, then exit and immediately execute the cancel action (exit) if the oldtype is not NULL, the cancel action type value will be stored in the shipment. int pthread_setcanceltype( int type, int *oldtype) Indicates that after receiving the signal, the CANCLED state is set and the CANCEL signal is ignored to continue running if old_state is not NULL, it will be stored in the original Cancel state for recovery.Ĭ. Setting the thread's response to Cancel signals, state has two values: PTHREAD_CANCEL_ENABLE (default) and PTREAD_CANCEL_DISABLE. int pthread_setcancelstate( int state, int *oldstate) Successful sending does not mean that threads terminate.ī. Send a termination signal to the thread thread and return 0 if successful, otherwise it is not zero.