我写了一个小程序。如下:
#include <pthread.h>
#include <limits.h>#define Thread_NUM 5void *MultiThread_soap_serve(){sleep(5) printf("new pthread!!\n")}//PTHREAD_STACK_MIN 经过计算是16K。//64*16K = 1M,线程堆栈应该是够用的。#define MICHAEL_SET_PTHREAD_STACK_SIZE 64int main(){pthread_attr_t attr pthread_attr_init(&attr) size_t stacksize = MICHAEL_SET_PTHREAD_STACK_SIZE*PTHREAD_STACK_MIN //stacksize =PTHREAD_STACK_MIN //stackaddr=(void*)malloc((N+1)*PTHREAD_STACK_MIN) //pthread_attr_getstack(&attr,&statckattr,&stacksize) //pthread_attr_setstack(&attr,stackaddr,) pthread_attr_setstacksize(&attr,stacksize) int iThreadNum = 0 pthread_t PSoapThread[Thread_NUM] for ( iThreadNum <Thread_NUM iThreadNum++ ){pthread_create(&PSoapThread[iThreadNum],&attr,MultiThread_soap_serve,(void *)NULL) }pthread_attr_destroy(&attr) while(1){sleep(10) printf("main!!\n") }}
Thread(ThreadGroup group, Runnable target, String name, long stackSize)分配新的 Thread 对象,以便将 target 作为其运行对象,将指定的 name 作为其名称,作为 group 所引用的线程组的一员,并具有指定的堆栈大小。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)