Linux ????????????Linux??
???????????? ???????[ 2016/7/12 13:47:29 ] ?????????????? ???
?????????????1????2????3?????????????????е?????????
????netsky@ubuntu:~/workspace/pthead_test$ gcc -lpthread test.c
???????????????????pthread???е???????????#include????????????м???-lpthread ??
????netsky@ubuntu:~/workspace/pthead_test$ ./a.out
????enter main
????enter thread2
????this is thread2?? g_Flag: 0?? thread id is 3079588720
????this is thread1?? g_Flag: 2?? thread id is 3079588720
????leave thread2
????leave main
????enter thread1
????this is thread1?? g_Flag: 2?? thread id is 3071196016
????this is thread1?? g_Flag: 1?? thread id is 3071196016
????leave thread1
???????????н??????????????????п??????
????netsky@ubuntu:~/workspace/pthead_test$ ./a.out
????enter main
????leave main
????enter thread1
????this is thread1?? g_Flag: 0?? thread id is 3069176688
????this is thread1?? g_Flag: 1?? thread id is 3069176688
????leave thread1
???????????
????netsky@ubuntu:~/workspace/pthead_test$ ./a.out
????enter main
????leave main
??????????????????????????????????main???????????????thread1??thread2?????????ü???????????????????????????????????????п??????????????????????е??????????????????????main??????????sleep()????????????thread1??thread2???ü???С?
????Attention:??????????????????????????thread1()??thread2()???????????????pthread_exit????????????exit()???????return??????????????????????
????pthread_exit()???????????????????????????????????????pthread_join???????????????????????
????return???????????????????return????????????
????exit??????????????????????е???exit???????е????к????????????
????“4?? ?????1????????2???????????”??4??????????????????thread1??????????????pthread_joinOK???
????4????????????
???????????????????????????????4????????????????????g_Flag????????????????thread1??thread2?????????????в???????????????м?????????thread1??thread2??????????С??????????????μ???????——????????
????????????
????????????????????????????????С????????????????????????????????д???????????????????????????????????????????
?????????????????????????£?
????#include int pthread_mutex_lock(pthread_mutex_t * mptr);
????int pthread_mutex_unlock(pthread_mutex_t * mptr);
????//Both return: 0 if OK?? positive Exxx value on error
????????????????в????????pthread_mutex_lock????????????????pthread_mutex_unlock???????????????????????????pthread_mutex_t??????????????????????????????????????????5???
????5????????????
??????5??——?????????g_Flag??1???2???????2???1???????????????????????????????????????????????
??????????????
????????????????????????????????????????????????????????????????????????????á????????????????????????????????????е??
???????????????????????????????????????????????????????????仯???????????????????????????????????????????????????????????????????????????????????????2?????
????????
??????λ????????
????????????????
??????????????£??????????????????????????????
??????????????д???????з????
?????????Э?????????
????“??????????????????????????????????????????????????”?????????5???????main????????????g_Flag??1???2???????2???1?????????????????????£?
????#include int pthread_cond_wait(pthread_cond_t *cptr?? pthread_mutex_t *mptr);
????int pthread_cond_signal(pthread_cond_t *cptr);
????//Both return: 0 if OK?? positive Exxx value on error
????pthread_cond_wait????????????????????棬pthread_cond_signal????????????????????????????????????????????????????????????pthread_cond_t???????????????????????????????
??????????????????????????????????????????????????????????????????е???????“???????????”??????????????乲???????????????????????????????????????????????ú???
?????????pthread_cond_wait?????????????????????????????????????????е???????????????????????????
????int pthread_cond_broadcast (pthread_cond_t * cptr);
?????????????棬?????????????????????????????????檔?????????????????????????
????int pthread_cond_timedwait (pthread_cond_t * cptr?? pthread_mutex_t *mptr?? const struct timespec *abstime);
????????????????????????????棬??????????????ETIME??
????6???????????
???????????????????????????д??????????????????
/*
??????POSIX?????????????????????д??????????1????
1?????int????????g_Flag?????0??
2????????????????1?????“this is thread1”??????g_Flag?????1
3?????????????????2?????“this is thread2”??????g_Flag?????2
4???????1????????2???????????
5???????????g_Flag??1???2???????2???1????????
*/
#include
#include
#include
#include
#includetypedef void* (*fun)(void*);
int g_Flag=0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* thread1(void*);
void* thread2(void*);
/*
* when program is started?? a single thread is created?? called the initial thread or main thread.
* Additional threads are created by pthread_create.
* So we just need to create two thread in main().
*/
int main(int argc?? char** argv)
{
printf("enter mainn");
pthread_t tid1?? tid2;
int rc1=0?? rc2=0;
rc2 = pthread_create(&tid2?? NULL?? thread2?? NULL);
if(rc2 != 0)
printf("%s: %dn"??__func__?? strerror(rc2));
rc1 = pthread_create(&tid1?? NULL?? thread1?? &tid2);
if(rc1 != 0)
printf("%s: %dn"??__func__?? strerror(rc1));
pthread_cond_wait(&cond?? &mutex);
printf("leave mainn");
exit(0);
}
/*
* thread1() will be execute by thread1?? after pthread_create()
* it will set g_Flag = 1;
*/
void* thread1(void* arg)
{
printf("enter thread1n");
printf("this is thread1?? g_Flag: %d?? thread id is %un"??g_Flag?? (unsigned int)pthread_self());
pthread_mutex_lock(&mutex);
if(g_Flag == 2)
pthread_cond_signal(&cond);
g_Flag = 1;
printf("this is thread1?? g_Flag: %d?? thread id is %un"??g_Flag?? (unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
pthread_join(*(pthread_t*)arg?? NULL);
printf("leave thread1n");
pthread_exit(0);
}
/*
* thread2() will be execute by thread2?? after pthread_create()
* it will set g_Flag = 2;
*/
void* thread2(void* arg)
{
printf("enter thread2n");
printf("this is thread2?? g_Flag: %d?? thread id is %un"??g_Flag?? (unsigned int)pthread_self());
pthread_mutex_lock(&mutex);
if(g_Flag == 1)
pthread_cond_signal(&cond);
g_Flag = 2;
printf("this is thread2?? g_Flag: %d?? thread id is %un"??g_Flag?? (unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
printf("leave thread2n");
pthread_exit(0);
}
???????????п?????????????????
???????????????????????漰???????????????????SPASVOС??(021-61079698-8054)?????????????????????????
??????
Linux??????DDR????????????Linux?????????????????豸??????????linux???в???????????м??????????????????????????Linux????Linux??DNS????????????γ???????????Linux?????????????Linux????(???)????????Linux ????????????????????????????????Windows???У?Linux???????Linux????????????????Linux iommu??vfio???????Linux????ε???TCP???????Linux?????????????СLinux?·???????????Linux???Jexus?й?Asp.Net Core??ó???Linux?????????Щ??
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11????????
?????????App Bug???????????????????????Jmeter?????????QC??????APP????????????????app?????е????????jenkins+testng+ant+webdriver??????????????JMeter????HTTP???????Selenium 2.0 WebDriver ??????