|
|
|
|
| ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð |
 ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - zhengdalx [ 2005-03-30 22:54 | 3,409 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - vxworkslinux [ 2005-03-31 13:27 | 323 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - zhengdalx [ 2005-03-31 23:54 | 331 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - vxworkslinux [ 2005-04-01 22:30 | 5,930 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - zhengdalx [ 2005-04-03 23:38 | 138 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - zhengdalx [ 2005-04-02 00:14 | 134 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - zhengdalx [ 2005-03-31 23:51 | 2,290 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - zhengdalx [ 2005-03-31 23:40 | 100 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - vxworkslinux [ 2005-04-01 19:26 | 3,011 byte(s)]
 Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð - zhengdalx [ 2005-04-02 00:03 | 142 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
#include <iostream>
using namespace std;
#include <pthread.h>
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
bool flag=true;
void *thread_run(void *)
{
int state;
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
cout<<"------------send a signal"<<endl;
pthread_mutex_unlock(&mutex);
cout<<"------------thread start"<<endl;
while(true){
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);
cout<<"-----------thread loop"<<endl;
for(int i=0 ; i < 10000 ; i++);
cout<<"----------start to sleep"<<endl;
pthread_setcancelstate(state , &state);//enable to cancel the thread
sleep(1);
}
}
int main()
{
for(int i=0 ; i < 1000 ; i++){
pthread_t id;
cout<<"create a thread:"<<i<<endl;
pthread_mutex_lock(&mutex);
pthread_create(&id , NULL , thread_run , NULL);
cout<<"start to wait"<<endl;
pthread_cond_wait(&cond , &mutex);
pthread_mutex_unlock(&mutex);
//cout<<"wait for the new thread to run"<<end;
//pthread_cond_wait(&cond , &mutex);
cout<<"cancel a thread"<<endl;
pthread_cancel(id);
void *reval;
cout<<"wait for a thread to die"<<endl;
pthread_join(id , &reval);
cout<<endl<<endl;
}
}
Èç¹û°Ñthread_run¸Ä³ÉÈçÏ£º
void *thread_run(void *)
{
int state;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
cout<<"------------send a signal"<<endl;
pthread_mutex_unlock(&mutex);
cout<<"------------thread start"<<endl;
while(true){
cout<<"-----------thread loop"<<endl;
for(int i=0 ; i < 10000 ; i++);
cout<<"----------start to sleep"<<endl;
pthread_setcancelstate(state , &state);//enable to cancel the thread
sleep(1);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);//disable to cancel the
thread
}
}
ÎÒ·¢ÏÖºÃÏñÖ»ÓÐÔÚµ÷ÓÃÏñsleep()ÕâÑùµÄº¯Êý(»áÈÃÏß³Ì¹ÒÆðµÄº¯Êý)µÄʱºò±»cancelµô£¬²ÅÄÜÕû¸ö³
ÌÐò²ÅÄÜÕý³£ÔËÐС£ÎÒ²»Ã÷°×Ϊʲô»áÕâÑù¡£
Çë´óÅ£Ö¸½Ì
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
ÉϸöÌû×ÓÄãÎʵÄÒ²ÊǶàÏ̱߳à³Ì£¬ÇëÔÁÂÎÒÉϸöÌû×ӻصúܲÝÂÊ
ºóÀ´ÎÒ°ÑÄãÉϸöÌû×ӵijÌÐò²âÊÔÁËһϣ¬·¢ÏÖûÎÊÌ⣬ÎÒÒÔΪÄãÒѾŪÇå³þÁË£¬ËùÒÔ¾ÍûÓÐÔÙ¸úÌû¡£
Õâ´ÎÄãÓÖ·¢Ìû×ÓÎʹØÓÚ¶àÏ̵߳ÄÎÊÌ⣬ÎÒ»¹ÊÇû¿´¶®ÄãÒªÎÊʲô¡£ºÃÏñÊǹØÓÚÍ˳öµãµÄÎÊÌ⣨ÎҲµģ©£¬
Äã¿ÉÒÔmanһϣ¬ÀïÃæÓйØÓÚÍ˳öµãµÄ¸ÅÄҲÐí»á¶ÔÄãÓÐËù°ïÖú
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
ÎÒµÄÒâ˼¾ÍÊÇÔÚµÚÒ»¸öthread_run()º¯ÊýÖÐ
void *thread_run(void *)
{
int state;
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
cout<<"------------send a signal"<<endl;
pthread_mutex_unlock(&mutex);
cout<<"------------thread start"<<endl;
while(true){
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);
cout<<"-----------thread loop"<<endl;
for(int i=0 ; i < 10000 ; i++);
cout<<"----------start to sleep"<<endl;
pthread_setcancelstate(state , &state);//enable to cancel the thread
sleep(1);
}
}
³ÌÐòÔÚÔËÐе½º¯ÊýµÄǰ°ë²¿·ÖµÄʱºò¿ÉÒÔ±»pthread_cancel()ÖÕÖ¹µôµÄ£¬ËùÒÔ³ÌÐòÔËÐеÄʱºòд´½¨µÄÏ߳̾³£²»Äܱ»Ë³ÀûµÄÖÕÖ¹µô
¶øÈç¹ûÎҰѳÌÐò¸Ä³ÉÏÂÃæµÄÑù×Ó
void *thread_run(void *)
{
int state;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
cout<<"------------send a signal"<<endl;
pthread_mutex_unlock(&mutex);
cout<<"------------thread start"<<endl;
while(true){
cout<<"-----------thread loop"<<endl;
for(int i=0 ; i < 10000 ; i++);
cout<<"----------start to sleep"<<endl;
pthread_setcancelstate(state , &state);//enable to cancel the thread
sleep(1);
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);//disable to cancel thethread
}
}
µÄʱºò£¬ÓÉÓÚеÄÏß³ÌÖ»ÓÐÔÚÔËÐе½sleepµÄʱºò²ÅÓпÉÄܱ»pthread_cancelÖÕÖ¹µô¡£Ææ¹ÖµÄÊÇ£¬Èç¹ûÊÇÓÃÏÂÃæµÄÕâ¸öthread_run¾Í²»»áÓÐÎÊÌâÁË¡£
ÎÒÊDz»Ã÷°×Ϊʲô»áÕâ¸öÑù×Ó
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
ÎҸоõд´½¨µÄÏ̲߳»Äܱ»Ë³ÀûÖÕÖ¹µÄʱºòºÃÏñÏ̵߳÷¶ÈµÄÇé¿öÈçÏ£¬¼ÙÈç
ÓÐÁ½¸öÏß³ÌA,B£¬
Ïß³ÌAÖ´ÐÐpthread_cancel()£¬ÏëÖÕÖ¹Ïß³ÌB
µ÷¶Èµ½Ïß³ÌBÖ´ÐÐ
Ïß³ÌB±»ÖÕÖ¹
Ïß³ÌAµ÷ÓÃpthread_join()£¬Ïß³ÌAÓÀÔ¶±»×èÈû
Õâ¸öÊÇÎҵĸöÈËÏë·¨£¬²»ÖªµÀ¶Ô²»¶Ô
²»ÖªµÀÏÖÔÚÄãÓÐûÓÐŪÇå³þÎÒµÄÒâ˼£¿
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
ÉϸöÌù×ÓÄãµÄÔ´ÂëÈçÏÂ:
#include <iostream>
using namespace std;
#include <pthread.h>
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
void *thread_run(void *)
{
cout<<"------------thread start"<<endl;
//pthread_cond_signal(&cond);
int state;
while(true){
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);
cout<<"-----------thread loop"<<endl;
for(int i=0 ; i < 10000 ; i++);
cout<<"----------start to sleep"<<endl;
pthread_setcancelstate(state , &state);
sleep(1);
}
}
int main()
{
for(int i=0 ; i < 1000 ; i++){
pthread_t id;
cout<<"create a thread"<<endl;
pthread_create(&id , NULL , thread_run , NULL);
cout<<"cancel a thread"<<endl;
pthread_cancel(id);
void *reval;
cout<<"wait for a thread to die"<<endl;
pthread_join(id , &reval);
cout<<endl<<endl;
}
}
ÎÒÖ´ÐеĽá¹ûÈçÏÂ:(gcc version 3.3.3 20040412 (Red Hat Linux 3.3.3-7))
create a thread
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
wait for a thread to die
create a thread
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
wait for a thread to die
create a thread
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
wait for a thread to die
create a thread
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
wait for a thread to die
create a thread
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
wait for a thread to die
create a thread
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
wait for a thread to die
create a thread
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
wait for a thread to die
.......
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
ÎÒ²âÊÔÕâ¸öÌù×ӵĵÚÒ»ÖÖ·½°¸(¸ÄÁ˵㶫Î÷,Äã¿´¿´),
#include <iostream>
using namespace std;
#include <pthread.h>
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
bool flag=true;
#if 1
void *thread_run(void *)
{
int state;
pthread_mutex_lock(&mutex);
pthread_cond_signal(&cond);
cout<<"------------send a signal"<<endl;
pthread_mutex_unlock(&mutex);
//printf("test!
");-----------------------------------------------------(1)
cout<<"------------thread start"<<endl;
while(true){
// pthread_testcancel();---------------------------------------------(0)
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE , &state);
cout<<"-----------thread loop"<<endl;
for(int i=0 ; i < 10000 ; i++);
cout<<"----------start to sleep"<<endl;
pthread_setcancelstate(state , &state);//enable to cancel the thread
sleep(1);
}
}
#endif
int main()
{
for(int i=0 ; i < 1000 ; i++){
pthread_t id;
cout<<"create a thread:"<<i<<endl;
pthread_mutex_lock(&mutex);
pthread_create(&id , NULL , thread_run , NULL);
cout<<"start to wait"<<endl;
pthread_cond_wait(&cond , &mutex);
pthread_mutex_unlock(&mutex);
//cout<<"wait for the new thread to run"<<end;
//pthread_cond_wait(&cond , &mutex);
//usleep(1);-------------------------------------------------------(2)
cout<<"cancel a thread"<<endl;
if( pthread_cancel(id) == 0 )
{
perror("pthread_cancel:error");
}
void *reval;
cout<<"wait for a thread to die"<<endl;
if( pthread_join(id , &reval) == 0 )
{
perror("pthread_join:error");
}
cout<<endl<<endl;
}
}
Êä³öÈçÏÂ:
create a thread:0
start to wait
------------send a signal
cancel a thread
pthread_cancel:error: Success
wait for a thread to die
FATAL: exception not rethrown
------------thread start
ÒÑ·ÅÆú
Õâ¸ö˵Ã÷pthread_cancel³É¹¦,»¹Ã»ÓÐÖ´Ðе½pthread_join
Ê×ÏÈÎÒ»³ÒÉÊÇÍ˳öµãµÄÎÊÌâ,ËùÒÔÔÚ(0)´¦¼ÓÁËpthread_testcancel,µ«ÊÇÊä³öÒÀÈ»´íÎó(ÈçÉÏ)
ºóÀ´ÎÒ¼ÓÉÏ(1),Ö´ÐоÍûÎÊÌâ,µ«ÊÇÊä³ö²»ÊÇSuccess
½á¹ûÈçÏÂ:
create a thread:0
start to wait
------------send a signal
cancel a thread
pthread_cancel:error: Success
wait for a thread to die
pthread_join:error: Illegal seek
test!
create a thread:1
start to wait
------------send a signal
cancel a thread
pthread_cancel:error: Illegal seek
wait for a thread to die
pthread_join:error: Illegal seek
test!
create a thread:2
start to wait
------------send a signal
cancel a thread
pthread_cancel:error: Illegal seek
wait for a thread to die
pthread_join:error: Illegal seek
test!
create a thread:3
start to wait
------------send a signal
cancel a thread
pthread_cancel:error: Illegal seek
wait for a thread to die
pthread_join:error: Illegal seek
test!
.......
×îºóÈ¥µô(1),¼ÓÉÏ(2):Ö´ÐоÍҲûÎÊÌâ,µ«ÊÇÊä³öÒ²²¢²»ÊÇSuccess,Óë(1)²»Í¬µÄÊÇ:(2)ÔÚsleep´¦Í˳ö,(1)ÔÚprintf´¦Í˳ö
½á¹ûÈçÏÂ:
create a thread:0
start to wait
------------send a signal
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
pthread_cancel:error: Success
wait for a thread to die
pthread_join:error: Illegal seek
create a thread:1
start to wait
------------send a signal
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
pthread_cancel:error: Illegal seek
wait for a thread to die
pthread_join:error: Illegal seek
create a thread:2
start to wait
------------send a signal
------------thread start
-----------thread loop
----------start to sleep
cancel a thread
pthread_cancel:error: Illegal seek
wait for a thread to die
pthread_join:error: Illegal seek
......
ÔÒòÎÒÒ²²»Ì«Çå³þ,ÇëÅ£ÈËÖ¸µã
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
²»»á°É¡£
ΪʲôÎÒÕâ±ßÓÐÎÊÌ⣿
ÿ´ÎÖ÷Ïß³ÌÔÚд´½¨µÄÏ̱߳»µ÷ÓÃ֮ǰµ÷ÓÃÁËpthread_cancel()£¬³ÌÐò¾Í»á³ö´íµÄ
Ì«Ææ¹ÖÁ˰ɡ£
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
°´ÕÕPosix thread£¬printf()Ó¦¸Ã²»ÊÇÒ»¸öÍ˳öµã°É£¿
»¹ÓУ¬ÎҵĵÚÒ»¸ö³ÌÐòÕæµÄûÓÐÎÊÌâÂð£¿ÎªÊ²Ã´ÎҸĽø¹ýµÄÄǸö³ÌÐò·´¶ø»áÓÐÎÊÌâÄØ£¿
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
¸Õ²ÅÊÔÁËһϣ¬È·ÊµÈç¹ûÔÚprintf()Í˳öµÄ»°pthread_join¾Í»á×èÈûµÄ
printf²»Ò»¶¨»áÒýÆðϵͳµ÷Óõģ¬ÎªÊ²Ã´Ã¿´ÎÔËÐе½ÕâµÄʱºò¶¼»á³ö´íÄØ£¿
|
|
[Original]
[Print]
[Top]
|
|
|