- 论坛徽章:
- 0
|
我的程序和运行结果
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#include<unistd.h>
#include<pthread.h> /* Linux线程库头文件 */
void * start(void * tempc);
int main()
{
pthread_t tid1, tid2;
/* 创建两个线程 */
//EXEC SQL ENABLE THREADS;
pthread_attr_t attr;
/*初始化属性值,均设为默认值*/
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
while(1)
{
static int i=2;
if( pthread_create( &tid1,&attr,&start,NULL ) )
{
printf( "创建线程失败!thread ID1:%d,Err No:%d,%s\n",tid1,errno,strerror(errno) );
i--;
if (i==0) exit(1);
}
else
{
//close(tid1);
//printf("thread 1 finished,tid1:%l\n",tid1);
//pthread_join( tid1,NULL );
}
//pthread_detach(tid1);
/* 等待线程退出 */
//if( pthread_join( tid1,NULL ) )
//{
// printf( "等待线程结束失败!\n" );
// exit(1);
//}
if( pthread_create( &tid2,NULL,start,NULL ) )
{
printf( "创建线程失败!thread ID2:%d,Err No:%d,%s\n",tid2,errno,strerror(errno) );
i--;
if (i==0) exit(1);
}
else
{
//printf(" thread 2 finished,tid2:%ul\n",tid2);
}
}
exit(0);
}
void * start(void *tempc)
{
int oldstate;
pthread_detach(pthread_self());
//fprintf(stderr,"Starting second child tid=%d\n",pthread_self());
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,&oldstate); /*Enable cancellation again*/;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,&oldstate);
for(int i=0;i++;i<=100000);
if(unsigned long THcnt=(pthread_self()%100000) ==0)
printf("thread:%u\n",pthread_self());
//sleep(1);
pthread_cancel(0);
pthread_exit(0);
return 0;
}
$ uname -a
HP-UX hostname01 B.11.11 U 9000/800 987654321 unlimited-user license
$ ./mth
thread:100000
thread:200000
thread:300000
thread:400000
...........
thread:2800000
创建线程失败!thread ID2:2896321,Err No:2,No such file or directory
创建线程失败!thread ID2:2896323,Err No:2,No such file or directory
[ 本帖最后由 microyong 于 2008-1-16 12:43 编辑 ] |
|