Node_Pointer
Limp Gawd
- Joined
- Mar 26, 2002
- Messages
- 313
I need some help.
I am using threads in the C language. I want the variable integer books to decrease by one after every time the thread is created. The problem is that 100 is descrease in the thread but does not "STICK" in the main program. I thought that the pthread_create passed args by reference in which case the value should "STICK" in the main section of program.
any way here is code....
I am using threads in the C language. I want the variable integer books to decrease by one after every time the thread is created. The problem is that 100 is descrease in the thread but does not "STICK" in the main program. I thought that the pthread_create passed args by reference in which case the value should "STICK" in the main section of program.
any way here is code....
Code:
#include <stdio.h>
#include <pthread.h>
void thread_function(void *ptr);
// Main Function
main(int argc, char *argv[])
{
int *books = 100;
// Create identifier variable for thread
pthread_t thread;
// Start thread using thread_function
pthread_create( &thread, NULL, (void*)& thread_function, ( void *) books );
// Output string from main function to standard output
printf("Hello");
// Exit thread
printf("%d", books);
pthread_exit(0);
}
// Thread Function
void thread_function( void *ptr)
{
// Output string
printf("hello from thread \n");
int *b;
(char *) ptr = ((char *) ptr - 1);
printf("%d", (char *) ptr);
// Exit thread
pthread_exit(0);
}