????1.?????????
????????????????????????????????????????????????????????????????е????????С??λ?????????????????У???????е??????????λ???????????????????????????????????????????п????????????????????????в????????
???????????????????????????????飬???????????????????????????????????????????????£??????????????????
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
????linux????????÷???POSIX?????????????????POSIX???????????????????????API??
????2. ?????
??????????????ID???????????????????ID????????????????ID?????????????????????????????????????ID??????????????в??????塣???ID??pthread_t?????
????//pthread_self?????????????ID
????include <pthread.h>
????pthread_t pthread_self(void);
?????ж????????ID???С??????κ????????????????????ж??????????????ID?????????????????
????//pthread_equal???t1??t2??????????ID?????????0?????????0???
????include <pthread.h>
????int pthread_equal(pthread_t t1?? pthread_t t2);
????3. ??????
???????????????????????????????????????????????????
????#include <pthread.h>
????int pthread_create(pthread_t *thread?? const pthread_attr_t *attr?? void *(*start_routine) (void *)?? void *arg);
??????????????
????thread(???????)????pthread_create????????????????????????t???????????????API???????????????????
????start_routine(???????)?????????????????
????arg(???????)?????????????????????????
????attr(???????)?????????????????????????С????????NULL??????????????????
???????????????
?????????????0??
????????????????????
??????????
?????????????????????????????????????main??????
??????????????????????????????????п????????????У?????????????????????????????У???п????????????????pthread_create?????????У?????????pthread_create???????????????????????????
?????????????
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void printids(const char *s){
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("%s?? pid %lu tid %lu (0x%lx) "??s??(unsigned long)pid??(unsigned long)tid??
(unsigned long)tid);
}
void *thread_func(void *arg){
printids("new thread: ");
return ((void*)0);
}
int main() {
int err;
pthread_t tid;
err = pthread_create(&tid??NULL??thread_func??NULL);
if (err != 0) {
fprintf(stderr??"create thread fail. ");
exit(-1);
}
printids("main thread:");
sleep(1);
return 0;
}
?????????????????У???????????????????????????????????????????????????????????????????У??????????sleep????????????β???????????????
????????????
????gcc -o thread thread.c -lpthread
???????н?????£?
????main thread:?? pid 889 tid 139846854309696 (0x7f30a212f740)
????new thread: ?? pid 889 tid 139846845961984 (0x7f30a1939700)
??????????????????????ID?????????乲??????е??????
????4. ??????
??????????????????????????????????????
?????????????????????
?????????????????????????????exit??_Exit????_exit?????????????????????????????????y????????????????
??????????????pthread_cancel?????????????
??????????????????????
?????????????????????return???main????(???????????)???return???????????????????????????y????????????????
??????????pthread_exit??????main????(???????????)????pthread_exit?????? ????????????????y???????????????????????????????????????????????????С?
????????????????
????include <pthread.h>
????void pthread_exit(void *retval);
??????????pthread_exit?????????????????????????????retval??????????
???????:retval?????????????????????????????
????5. ???????????
????5.1 ????????
???????????????????????????????????????????????????????????ID?????????????????Щ??????pthread_join??????????????????????ù??????????????
????include <pthread.h>
????int pthread_join(pthread_t thread?? void **retval);
?????????????
????thread(???????)????????????????????
????retval(???????)????????????????????????????????????????return??????????pthread_exit?????????
???????????
??????????????0
?????????????????????????
?????????X???????Y?????????Y???????У??????X????????????Y???????????Y????????????????????????X???????????????????
????????????????????????????壬????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
???????:????????????????????????????????????? ??????????????????