hi,

if (CLIENT_HANDLER_THREAD_CREATION_POLICY == POLICY_PRE_CREATE)
        {
                int id[NUMTHREADS];
                for (int i = 0;i < NUMTHREADS;i++)
                {
                        int id = i;
                        int status = pthread_create(&client_request_handler_threads[i], NULL, handle_client_requests, &id[i]);
                }
        }
 
void* handle_client_requests(void* id)
{
	//printf("server threads spawnd\n");
	// create log file

	thread_id = pthread_self();
	sprintf(log_file_name,"t%d.log",thread_id);
	log_file = fopen(log_file_name, "w");
	// todo: dynamically allocate storage
	char log_msg[5000];

	int tid = *((int*)id);
	printf("id: %d\n",tid);
	sprintf(log_msg,"thread id: %d\n",tid);
	write_to_log_file(log_msg);

}

I am not sure why the output is:
id: -718438848
id: 32563
id: -1205030144
id: -1213790742

from where did this garbage values come from?

Thanks.

johndoe444> from where did this garbage values come from?

From int id[NUMTHREADS]; that is not initialized.

hi,

if (CLIENT_HANDLER_THREAD_CREATION_POLICY == POLICY_PRE_CREATE)
        {
                int id[NUMTHREADS];
                for (int i = 0;i < NUMTHREADS;i++)
                {
                        int id = i;
                        int status = pthread_create(&client_request_handler_threads[i], NULL, handle_client_requests, &id[i]);
                }
        }
 
void* handle_client_requests(void* id)
{
	//printf("server threads spawnd\n");
	// create log file

	thread_id = pthread_self();
	sprintf(log_file_name,"t%d.log",thread_id);
	log_file = fopen(log_file_name, "w");
	// todo: dynamically allocate storage
	char log_msg[5000];

	int tid = *((int*)id);
	printf("id: %d\n",tid);
	sprintf(log_msg,"thread id: %d\n",tid);
	write_to_log_file(log_msg);

}

I am not sure why the output is:
id: -718438848
id: 32563
id: -1205030144
id: -1213790742

from where did this garbage values come from?

Thanks.

I figured it all out. It was because id at line 6-7 was local variables. After making them global it is working now.

johndoe444> from where did this garbage values come from?

From int id[NUMTHREADS]; that is not initialized.

Sorry You are right. I did not see your post before posting my second message.

Do not use same name as global and local variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.