threads with C.... help

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....



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);

}
 
Yeah I havent had the time yet to mess with pthreads but I got the thread to modify books and know of some sites that can help ya out hopefully. Seems that your process is finishing before the thread has a chance to do its thing.

Code:
#include <stdio.h>
#include <pthread.h>

void thread_function( void *ptr )
{
	int *books = (int *)ptr;
	*books = 50;
	sleep(10);
}

int main(void)
{
        int books = 100;
        pthread_t thread;

	// Start thread using thread_function
        pthread_create( &thread, NULL, (void *)thread_function, (void *) &books );
	sleep(5);
        printf("books %d\n", books);
	return 0;
}

Check out these sites,
http://www.cs.cf.ac.uk/Dave/C/CE.html
http://www.cs.nmsu.edu/~jcook/Tools/pthreads/library.html
 
Lord of Shadows probably hit the nail on the head. The overhead of getting new thread running is going to take longer than the printf calls in the main. Another option than using the sleep would be to put a while loop in and keep the program alive until you detected the change in value. If you put some form of output into the loop it would give you a feel for how long the thread took to do its thing.
 
"int *books = 100" is completely wrong. You're calling this a pointer, and setting the pointer to 100, which, when dereferenced, should probably give you a segfault. On top of this, you're never allocating any memory for the int that you expect to have there.


What you probably want is to declare "int books = 100" and then pass a pointer to that value (before you had a pointer but it didn't really reference anything). Then you want to pass the thread function "(void *) &books" in it's arguments.


...and I'm not even going to try to understand what you think is going on in the thread function. On the whole, it looks like you don't really understand pointers.

Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.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\n");

        // Exit thread

        sleep(2);
        printf("main sez : %d\n", books);
        pthread_exit(0);
}

// Thread Function

void thread_function( void *ptr)
{

        // Output string
        printf("hello from thread \n");
        *((int *)ptr) = *((int *)ptr)-1;

        printf("thread sez : %d\n", *((int *) ptr));
        // Exit thread
        pthread_exit(0);

}
 
Back
Top