c++ - calculating time taken to create n threads -
i trying create 1000 threads can know how time taking create them. im using pthreads. im getting segmentation fault: 11. googling tells me might case of using memory doubt possible reason.
any pointers might reason ?
code:
int main(int argc , char *argv[]) { int *i; // matti's answer below: ... = (int*)malloc(sizeof(int)); *i = 0; while( *i < 100) { pthread_t thread_id; puts("connection accepted"); if( pthread_create( &thread_id , null , connection_handler , (void*) &i) < 0) { error("could not create thread"); return 1; } //pthread_detach(thread_id); *i = *i + 1; } return 0; } void *connection_handler(void *i) { sleep(1); return 0; }
your problem fact you're dereferencing pointer never initialized:
int *i; *i = 0;
what's wrong int i;
?
Comments
Post a Comment