????5.2???????????????????
????????????????????????????????????????????????????????????????????????ν?????????????????????????????????????????????????δ?????????????????????????????????????????
???????????????????????????????????????????????
????#include <pthread.h>
????int pthread_mutex_init(pthread_mutex_t *mutex?? const pthread_mutexattr_t?? *mutexattr);
????int pthread_mutex_lock(pthread_mutex_t *mutex);
????int pthread_mutex_unlock(pthread_mutex_t *mutex);
????int pthread_mutex_destory(pthread_mutex_t *mutex);
???????????????????????????0??????????????????????Щ????????????errno???????????????????????м?顣????????????????????????????????ó?NULL??
?????????????????????д??????????????£?
???????????????
????gcc -D_REENTRANT thread4.c -o thread4 –lpthread
??????????????
$ ./thread4
Input some text. Enter 'end' to finish
123
You input 3 characters
1234
You input 4 characters
12345
You input 5 characters
end
You input 3 characters
Thread joined
????6.????????
???????????δ??????????????????????????????????????????????о??Щ?????????
????????????????????У??????????pthread_join????????????????Щ????£??????????????磺?????????????????????????????????????????????????????????????????????????б??????????????С???????????????????“???????”??
???????????????????????????
????(1)int pthread_attr_init (pthread_attr_t* attr);
?????????????????????????????
????attr??????????
????????????????????0??????-1
????(2) int pthread_attr_setscope (pthread_attr_t* attr?? int scope);
??????????????????????
????attr??????????
????scope??PTHREAD_SCOPE_SYSTEM(??)??PTHREAD_SCOPE_PROCESS(???)
????????????????????0??????-1
????(3) int pthread_attr_setdetachstate (pthread_attr_t* attr?? int detachstate);
????????????????????????
????attr??????????
????detachstate??PTHREAD_CREATE_DETACHED(????)??PTHREAD_CREATE_JOINABLE???????
????????????????????0??????-1
????(4) int pthread_attr_setschedpolicy(pthread_attr_t* attr?? int policy);
???????????????????????????
????attr??????????
????policy????????????SCHED_FIFO??SCHED_RR??SCHED_OTHER??
????????????????????0??????-1
????(5) int pthread_attr_setschedparam (pthread_attr_t* attr?? struct sched_param* param);
???????????????????????
????attr??????????
????param????????????
????????????????????0??????-1
????(6) int pthread_attr_destroy (pthread_attr_t* attr);
????????????????????????????
????attr??????????
????????????????????0??????-1
????(7)????
????int pthread_attr_setguardsize(pthread_attr_t* attr??size_t guardsize);//?????′????????????????С??
????int pthread_attr_setinheritsched(pthread_attr_t* attr?? int inheritsched);//????????????′???????????????
????int pthread_attr_setstack(pthread_attr_t* attr?? void* stackader??size_t stacksize);//??????????????????????????????С??磨????????λ????
????int pthread_attr_setstackaddr(pthread_attr_t* attr?? void* stackader);//???????′????????????????
????int pthread_attr_setstacksize(pthread_attr_t* attr?? size_t stacksize);//???????′??????????С??磨????????λ????
??????????????????10??????
pthread_attr_t attr;
struct sched_param param;
pthread_attr_init(&attr);
pthread_attr_setscope (&attr?? PTHREAD_SCOPE_SYSTEM); //??
pthread_attr_setdetachstate (&attr?? PTHREAD_CREATE_DETACHED); //????
pthread_attr_setschedpolicy(&attr?? SCHED_RR);
param.sched_priority = 10;
pthread_attr_setschedparam(&attr?? &param);
pthread_create(xxx?? &attr?? xxx?? xxx);
pthread_attr_destroy(&attr);
??????????????????????????????????????????????????????????????????????pthread_exit????????????????????????????κ???????????????£?
???????????????
????gcc -D_REENTRANT thread5.c -o thread5 –lpthread
??????????????
????$ ./thread5
????thread_function is running. Argument: hello world!
????Waiting for thread to finished...
????Waiting for thread to finished...
????Waiting for thread to finished...
????Waiting for thread to finished...
????Second thread setting finished flag?? and exiting now
????Other thread finished!
???????????????????????????????????????????????????????????????
????7.?????????
??????????????????????????????????????????????з???????????????????????????????????????????????????
??????????????????????????????????
????#include <pthread.h>
????int pthread_cancel(pthread_t thread);
????????????????????????????????????????????????????????
????????????pthread_setcancelstate????????????????
????#include <pthread.h>
????int pthread_setcancelstate(int state?? int *oldstate);
????????????
????state???????PTHREAD_CANCEL_ENABLE??????????????????????PTHREAD_CANCEL_DISABLE???????????
????oldstate??????????????????????????????????????????NULL???????????????????????????????????????Σ???pthread_setcanceltype????????????
????#include <pthread.h>
????int pthread_setcanceltype(int type?? int *oldtype);
????????????
????type??????PTHREAD_CANCEL_ASYNCHRONOUS??????????????????????????????ж????????????PTHREAD_CANCEL_DEFERRED??????????????????????????????????????????????????е??????????ж???pthread_join??pthread_cond_wait??pthread_cond_timedwait??pthread_testcancel??sem_wait??sigwait??
????oldtype???????????????????費?????????????????????NULL??
???????????£??????????????????PTHREAD_CANCEL_ENABLE???????????PTHREAD_CANCEL_DEFERRED??
???????±?д????thread6.c?????????????????????????????????
???????????????
????gcc -D_REENTRANT thread6.c -o thread6 –lpthread
??????????????
????$ ./thread6
????thread_function is running...
????Thread is still running (0)...
????Thread is still running (1)...
????Thread is still running (2)...
????Thread is still running (3)...
????Canceling thread...
????Waiting for thread to finished...
????8.?????
????????????????д????????涼????????????????????????????????????????????????????
???????????????
????gcc -D_REENTRANT thread7.c -o thread7 –lpthread
??????????????
$ ./thread7
thread_function is running. Argument was 0
thread_function is running. Argument was 1
thread_function is running. Argument was 2
thread_function is running. Argument was 3
thread_function is running. Argument was 4
Bye from 1
thread_function is running. Argument was 5
Waiting for threads to finished...
Bye from 5
Picked up a thread:6
Bye from 0
Bye from 2
Bye from 3
Bye from 4
Picked up a thread:5
Picked up a thread:4
Picked up a thread:3
Picked up a thread:2
Picked up a thread:1
All done
????9.С??
?????????????????Linux?????μ??????????????????????????????????????????????????????????????????????????
???????????????????????Linux?????????????á?