Pthread_create issue

Hello

My problem goes like this:

I have used Pthread_create, and I have tryed to create 2 proccess but nothing happens! It does not even matter what the function im trying to create do. It is if im trying to activate an empty function. This is my code.

Any help will be highly appreciated.

please note that the code does compile but actually does nothing.

my code is:

================================================
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include "semaphore.h"

#pragma warning(disable:4996)
//Disable c4496 warning, regarding the potentially unsafe
//methods in the Standard C++ Library.

int arr[5] = {0};
int i;
int j = 0;
int init = 1;

void* manufacturer(void* Thread_data)
{
for (i = init; i <= 100; i++)
{
arr[i-1] = i;
j++;
if (j == 4)
{
down(&semaphore1);

		j = 0;
		init = i;
		up\(&semaphore2\);
	\}	
\}

pthread_exit\(NULL\);

}

void* consumer(void* Thread_data)
{
if (arr[0] != 0)
{
for (i; i >= 0; i--)
{
if (arr [i]!= 0)
{
printf("%d",arr[i]);
arr [i]= 0; //Removing the number from the array.
}
}
}
else
{
down(&semaphore2);
up(&semaphore1);
}

pthread_exit\(NULL\);

}

int main()
{
int status; //Used to recieve the result of the thread creation.
init_sem(&semaphore1, 0);
init_sem(&semaphore2, 0);

pthread_t manufacturer_thread;	//The manufacturer thread's name.	pthread_t consumer_thread;	//The consumer thread's name.	
status = pthread\_create\(&manufacturer_thread, NULL, manufacturer, NULL\);

if \(status == 0\)
\{
	printf\("The manufacturer process has been successfully created\\n"\);
\}
else
\{
	printf\("Error: The manufacturer process could not be created\\n"\);
\}

status = pthread\_create\(&consumer_thread, NULL, consumer, NULL\);

if \(status == 0\)
\{
	printf\("The consumer process has been successfully created\\n"\);
\}
else
\{
	printf\("Error: The manufacturer process could not be created\\n"\);
\}

}

==========================================================
I know it's a little long, But I just dont know what to do. Im using the knoppix CD( im booting from it) so its under Linux OS.

when I try to run the application, I get:

The manufacturer process has been successfully created
The consumer process has been successfully created

and that's it! meaning that the thread has been created successfully, but the functions are never called..

thanks

  • First, enable the warning to see if you get some usefull messages.
  • Declare enough elements for arr[] as used for the functions manufacturers and consumer.
  • The 3th parameter should be an address of the thread function:
status = pthread_create(&manufacturer_thread, NULL, &manufacturer, NULL);
status = pthread_create(&consumer_thread, NULL, &consumer, NULL);

instead of:

status = pthread_create(&manufacturer_thread, NULL, manufacturer, NULL);
status = pthread_create(&consumer_thread, NULL, consumer, NULL);

Regards