Thread: Thread/mutex difficulty

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    70

    Thread/mutex difficulty

    How would i implement mutex with this code

    Would i pass buffer and mutex into a struct? If so, could someone show me how or reference a tutorial?

    Code:
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    
    void *print(void* string)
    {
            printf("%s\n", string);
    }
    
    int main(void)
    {
    
            char buffer[100];
            pthread_t thread;
    
            pthread_create(&thread, NULL, print, (void*)buffer);
    
            fgets(buffer, sizeof(buffer), stdin);
    
            pthread_join(thread, NULL);
    
    }
    Last edited by erasm; 09-09-2009 at 03:51 PM.

  2. #2

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    I am at a dead end with threads. How could i pass the buffer and a mutex to the child. Is the buffer suppose to be locked to avoid conflicts of variable access here? Do i lock the variable in the child function? Would this be the structure i pass into the child?

    Code:
    typedef struct myStruct
    {
            pthread_mutex_t mutex_a;
            char buffer[100];
    
    }myStruct;

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You haven't described your problem AT ALL, you've just vaguely stated that you cannot get something to work the way you want, but it's not clear what you want or in what it is that happens instead.

    I've used pthreads a bit, but never bothered with mutex's because I don't have any possible race conditions set up.

    Code:
    pthread_create(&thread, NULL, print, (void*)buffer);
    pthread_detach(thread);
    As long as print() is of type void* that should work fine.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    The mutex can be a global variable. So all functions can use it. Otherwise yeah, you have to pass it with a struct.

    No, the buffer isn't locked in any way. You have to do that, if needed. There isn't always the need to do so.

    Where is exactly your problem or confusion?

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    The problem is stdin is not being printed.

    The question asks to use a mutex to make the child wait for the parent to read a line before displaying it.

    Hint: you will need to pass both the buffer and the mutex tot he child.

    Also watchmen is leeeeeeeeeeeeet

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    You have bit confused the functions.

    pthread_create will create and run the thread. It will print the un-initialized buffer.
    pthread_join suspends the main thread until the child-thread is executed.

    So, you first need fgets() and then the pthread_create().
    Or do this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <pthread.h>
    
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    void *print(void* string)
    {
    	pthread_mutex_lock(&mutex);
            printf("%s\n", (char*)string);
    	pthread_mutex_unlock(&mutex);
    }
    
    int main(void)
    {
    
            char buffer[100];
            pthread_t thread;
    	pthread_mutex_lock(&mutex);
            pthread_create(&thread, NULL, print, (void*)buffer);
            fgets(buffer, sizeof(buffer), stdin);
    	pthread_mutex_unlock(&mutex);
    	pthread_join(thread, NULL);
    }

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    For my reference, how would you implement this with a struct?

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Using your struct. This will avoid global variables
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <pthread.h>
    
    typedef struct myStruct
    {
            pthread_mutex_t mutex_a;
            char buffer[100];
    
    } myStruct;
    
    void *print(void* data)
    {
            myStruct *d = (myStruct *)data;
    	pthread_mutex_lock(&(d.mutex_a ));
            printf("%s\n", d.buffer;
    	pthread_mutex_unlock(&(d.mutex_a ));
    }
    
    int main(void)
    {
            myStruct data;
            data.mutex_a = PTHREAD_MUTEX_INITIALIZER;
            pthread_t thread;
    	pthread_mutex_lock(&(data.mutex_a ));
            pthread_create(&thread, NULL, print, (void*)(&data));
            fgets(data.buffer, sizeof(data.buffer), stdin);
    	pthread_mutex_unlock(&(data.mutex_a ));
    	pthread_join(thread, NULL);
    }

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    That code gets these errors. How can i fix this?


    sync.c: In function âprintâ:
    sync.c:14: error: request for member âmutex_aâ in something not a structure or union
    sync.c:15: error: request for member âbufferâ in something not a structure or union
    sync.c:16: error: request for member âmutex_aâ in something not a structure or union
    Last edited by erasm; 09-13-2009 at 06:48 PM.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <pthread.h>
    
    typedef struct myStruct
    {
            pthread_mutex_t mutex_a;
            char buffer[100];
    
    } myStruct;
    
    void *print(void* data)
    {
            myStruct *d = (myStruct *)data;
            pthread_mutex_lock(&(d->mutex_a ));
            printf("%s\n", d->buffer);
            pthread_mutex_unlock(&(d->mutex_a ));
    }
    
    int main(void)
    {
            myStruct data;
            data.mutex_a = PTHREAD_MUTEX_INITIALIZER;
            pthread_t thread;
            pthread_mutex_lock(&(data.mutex_a ));
            pthread_create(&thread, NULL, print, (void*)(&data));
            fgets(data.buffer, sizeof(data.buffer), stdin);
            pthread_mutex_unlock(&(data.mutex_a ));
            pthread_join(thread, NULL);
    }
    This code now gets the error:
    error: expected expression before â{â token
    bafflezored i am

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    PTHREAD_MUTEX_INITIALIZER is an initializer, and as such should be used during initialization only, not for assignment. Either initialize your struct, or use pthread_mutex_init().

    Incidentally, those casts are unnecessary in C. Object pointers can be converted to void* and back without any need for explicit conversion.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Eh? Care to tell us what line? Compiles fine for me.

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    70
    How would i initialize the mutex?
    Last edited by erasm; 09-13-2009 at 08:49 PM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should have just said: "Hi, I don't know anything about threads, and I don't want to actually learn anything either. Please do it for me."

    It would have saved us all some time.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-06-2009, 06:56 PM
  2. Replies: 4
    Last Post: 08-23-2009, 09:47 PM
  3. got difficulty on this
    By gtr_s15 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2006, 09:37 AM
  4. homework giving difficulty
    By jjj in forum C Programming
    Replies: 5
    Last Post: 09-13-2002, 06:35 PM
  5. Game difficulty
    By pdstatha in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2002, 04:12 PM