URN Logo
UNIX Resources » Linux » China Linux Forum » C/C++±à³Ì°æ » µÚ35Ò³ » ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
announcement ÉùÃ÷: ±¾Ò³ÄÚÈÝΪÖйúLinuxÂÛ̳µÄÄÚÈݾµÏñ£¬ÎÄÕµİæÈ¨ÒÔ¼°ÆäËûËùÓеÄÏà¹ØÈ¨ÀûÊôÓÚÖйúLinuxÂÛ̳ºÍÏàÓ¦ÎÄÕµÄ×÷Õߣ¬Èç¹û×ªÔØ£¬Çë×¢Ã÷ÎÄÕÂÀ´Ô´¼°Ïà¹Ø°æÈ¨ÐÅÏ¢¡£
Resources
China Linux Forum(finished)
Linux Forum(finished)
FreeBSD China(finished)
linuxforum.net
  Òµ½çÐÂÎÅÓëÆÀÂÛ
  ×ÔÓÉÈí¼þÔÓ̸
  IT ÈËÉú
  LinuxÈí¼þ¿ìµÝ
  ·­Òë×÷·»
  LinuxͼÊéÓëÆÀÂÛ
  GNU Emacs/XEmacs
  Linux ÖÐÎÄ»·¾³ºÍÖÐÎÄ»¯
  Linux×ÀÃæÓë°ì¹«Èí¼þ
  Linux ¶àýÌåÓëÓéÀÖ°æ
  ×ÔÓÉÖ®´°Mozilla
  ±Ê¼Ç±¾µçÄÔÉϵÄLinux
  Gentoo
  Debian Ò»×å
  ÍøÂç¹ÜÀí¼¼Êõ
  Linux °²×°ÓëÈëÃÅ
  WEB·þÎñÆ÷ºÍFTP·þÎñÆ÷
  ÓòÃû·þÎñÆ÷ºÍÓʼþ·þÎñÆ÷
  Linux·À»ðǽºÍ´úÀí·þÎñÆ÷Ó¦ÓÃ
  Îļþ¼°´òÓ¡·þÎñÆ÷
  ¼¼ÊõÅàѵÓëÈÏÖ¤
  LinuxÄں˼¼Êõ
  Linux ǶÈë¼¼Êõ
  LinuxÉ豸Çý¶¯³ÌÐò
  Linux ¼¯Èº¼¼Êõ
  LINUXƽ̨Êý¾Ý¿â
  ÏµÍ³ºÍÍøÂ簲ȫ
  CPU Óë ±àÒëÆ÷
  ÏµÍ³¼ÆËãÑо¿ËùרÀ¸
  LinuxϵÄGUIÈí¼þ¿ª·¢
  C/C++±à³Ì°æ
  PHP ¼¼ Êõ
  Java&jsp¼¼Êõ
  Shell±à³Ì¼¼Êõ
  Perl ±à ³Ì
  Python ±à ³Ì
  XML/Web Service ¼¼Êõ
  ÓÀÔ¶µÄUnix
  FreeBSDÊÀ½ç
   
ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
 
 
 
 
 
 
 
 
 
 
Subject: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: zhengdalx    Posted: 2005-03-30 22:54    Length: 3,409 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]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: vxworkslinux    Posted: 2005-03-31 13:27    Length: 323 byte(s)
[Original] [Print] [Top]
ÉϸöÌû×ÓÄãÎʵÄÒ²ÊǶàÏ̱߳à³Ì£¬ÇëÔ­ÁÂÎÒÉϸöÌû×ӻصúܲÝÂÊ
ºóÀ´ÎÒ°ÑÄãÉϸöÌû×ӵijÌÐò²âÊÔÁËһϣ¬·¢ÏÖûÎÊÌ⣬ÎÒÒÔΪÄãÒѾ­ÅªÇå³þÁË£¬ËùÒÔ¾ÍûÓÐÔÙ¸úÌû¡£
Õâ´ÎÄãÓÖ·¢Ìû×ÓÎʹØÓÚ¶àÏ̵߳ÄÎÊÌ⣬ÎÒ»¹ÊÇû¿´¶®ÄãÒªÎÊʲô¡£ºÃÏñÊǹØÓÚÍ˳öµãµÄÎÊÌ⣨ÎҲµģ©£¬
Äã¿ÉÒÔmanһϣ¬ÀïÃæÓйØÓÚÍ˳öµãµÄ¸ÅÄҲÐí»á¶ÔÄãÓÐËù°ïÖú
[Original] [Print] [Top]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: zhengdalx    Posted: 2005-03-31 23:40    Length: 100 byte(s)
[Original] [Print] [Top]
²»»á°É
ÎÒÉÏÃæÌù³öÀ´µÄµÚÒ»¸öС³ÌÐò»áûÓдíÎó£¿
ΪʲôÎÒÕâ±ßÔËÐлá³ö´íÄØ£¿
[Original] [Print] [Top]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: zhengdalx    Posted: 2005-03-31 23:51    Length: 2,290 byte(s)
[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]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: zhengdalx    Posted: 2005-03-31 23:54    Length: 331 byte(s)
[Original] [Print] [Top]
ÎҸоõд´½¨µÄÏ̲߳»Äܱ»Ë³ÀûÖÕÖ¹µÄʱºòºÃÏñÏ̵߳÷¶ÈµÄÇé¿öÈçÏ£¬¼ÙÈç
ÓÐÁ½¸öÏß³ÌA,B£¬
Ïß³ÌAÖ´ÐÐpthread_cancel()£¬ÏëÖÕÖ¹Ïß³ÌB
µ÷¶Èµ½Ïß³ÌBÖ´ÐÐ
Ïß³ÌB±»ÖÕÖ¹
Ïß³ÌAµ÷ÓÃpthread_join()£¬Ïß³ÌAÓÀÔ¶±»×èÈû

Õâ¸öÊÇÎҵĸöÈËÏë·¨£¬²»ÖªµÀ¶Ô²»¶Ô
²»ÖªµÀÏÖÔÚÄãÓÐûÓÐŪÇå³þÎÒµÄÒâ˼£¿
[Original] [Print] [Top]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: vxworkslinux    Posted: 2005-04-01 19:26    Length: 3,011 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;

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]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: vxworkslinux    Posted: 2005-04-01 22:30    Length: 5,930 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;


#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]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: zhengdalx    Posted: 2005-04-02 00:03    Length: 142 byte(s)
[Original] [Print] [Top]
²»»á°É¡£
ΪʲôÎÒÕâ±ßÓÐÎÊÌ⣿
ÿ´ÎÖ÷Ïß³ÌÔÚд´½¨µÄÏ̱߳»µ÷ÓÃ֮ǰµ÷ÓÃÁËpthread_cancel()£¬³ÌÐò¾Í»á³ö´íµÄ
Ì«Ææ¹ÖÁ˰ɡ£
[Original] [Print] [Top]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: zhengdalx    Posted: 2005-04-02 00:14    Length: 134 byte(s)
[Original] [Print] [Top]
°´ÕÕPosix thread£¬printf()Ó¦¸Ã²»ÊÇÒ»¸öÍ˳öµã°É£¿
»¹ÓУ¬ÎҵĵÚÒ»¸ö³ÌÐòÕæµÄûÓÐÎÊÌâÂð£¿ÎªÊ²Ã´ÎҸĽø¹ýµÄÄǸö³ÌÐò·´¶ø»áÓÐÎÊÌâÄØ£¿
[Original] [Print] [Top]
Subject: Re: ÇëÎÊÏÂÃæµÄÕâÁ½¶Î¶àÏ̳߳ÌÐòµÄ²î±ð
Author: zhengdalx    Posted: 2005-04-03 23:38    Length: 138 byte(s)
[Original] [Print] [Top]
¸Õ²ÅÊÔÁËһϣ¬È·ÊµÈç¹ûÔÚprintf()Í˳öµÄ»°pthread_join¾Í»á×èÈûµÄ
printf²»Ò»¶¨»áÒýÆðϵͳµ÷Óõģ¬ÎªÊ²Ã´Ã¿´ÎÔËÐе½ÕâµÄʱºò¶¼»á³ö´íÄØ£¿
[Original] [Print] [Top]
« Previous thread
ÈçºÎÖ¸¶¨¶à¸öipµØÖ·ÖеÄÒ»¸ö·¢ËÍÊý¾Ý
C/C++±à³Ì°æ
µÚ35Ò³
Next thread »
½¨Á¢raw socket ¿ÉÒÔ×Ô¼º×é×°IP°üÍ·£¬µ«ÊÇÈç¹ûÄã¸Ä¶¯ÁË·ÖÆ¬±êÖ¾£¬¾Í·¢ËͲ»³É¹¦ÁË£¿
     

Copyright © 2007 UNIX Resources Network, All Rights Reserved.      About URN | Privacy & Legal | Help | Contact us
±¸°¸ÐòºÅ: ¾©ICP±¸05006143    webmaster: webmaster@unixresources.net
This page created on 2008-07-17 03:53:04, cost 0.071465969085693 ms.