arrays - how to handle synchronization using mutex in a child thread in c programming -
i continuing yesterday's help, , have added code in child thread. basically, when user enters stdin, child thread should read , return parent thread. however, after printing output, code should redirected child thread , wait user press enter, once user press enter, code should exited. working, have used sleep() , want use mutex(), when comment sleep(), following codes print first ("press enter") parent code prints actual input.
/*required header files added*/ #include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <unistd.h> /*this structure hold string of user input , lock variable has created*/ struct thread_main { char *buffer; char *bufferparent; pthread_mutex_t lock; pthread_mutex_t lock1; } td; /*it child thread, , store user value in buffer variable has been declared in thread_main structure*/ static void *thread(void *buff) { /*the pointer has assigned structure, can values of buffer array*/ struct thread_arguments *arg = buff; //the previous code pthread_mutex_unlock(&td.lock); pthread_mutex_destroy(&td.lock); sleep(1); // want ignore this. pthread_mutex_init(&td.lock, 0); pthread_mutex_lock(&td.lock); printf("press enter"); /*this code read buffer , check enter*/ pthread_mutex_unlock(&td.lock); pthread_mutex_destroy(&td.lock); return null; }
as noted in comment:
the size of
arg
in thread function size of pointer; apparently, you're using 32-bit machine or 32-bit build on 64-bit machine. you'll need create structure pointer , size, , pass pointer structure thread function.
this code works , illustrates mean:
#include <stdio.h> #include <pthread.h> struct thread_data { char *buffer; pthread_mutex_t lock; } td; struct thread_arg { char *buffer; size_t buflen; }; static void *thread(void *data) { struct thread_arg *arg = data; printf("%zd\n", arg->buflen); td.buffer = fgets(arg->buffer, arg->buflen, stdin); pthread_mutex_unlock(&td.lock); return null; } int main(void) { char buffer[128]; struct thread_arg arg = { buffer, sizeof(buffer) }; pthread_t thread_id; pthread_mutex_init(&td.lock, 0); pthread_mutex_lock(&td.lock); printf("enter sync command -- "); pthread_create(&thread_id, null, thread, &arg); pthread_mutex_lock(&td.lock); printf("message read parent- %s", td.buffer); pthread_join(thread_id, null); pthread_mutex_unlock(&td.lock); pthread_mutex_destroy(&td.lock); return 0; }
note cannot use copies of locks if same original lock. code doesn't check fgets()
returns line of data (a bug).
i'm not convinced lock necessary @ all. i'm not convinced struct thread_data
worth keeping. code works, relying on thread exiting before parent tries read buffer.
#include <stdio.h> #include <pthread.h> struct thread_arg { char *buffer; size_t buflen; }; static void *thread(void *data) { struct thread_arg *arg = data; printf("%zd\n", arg->buflen); fgets(arg->buffer, arg->buflen, stdin); return null; } int main(void) { char buffer[128]; struct thread_arg arg = { buffer, sizeof(buffer) }; pthread_t thread_id; printf("enter sync command -- "); pthread_create(&thread_id, null, thread, &arg); pthread_join(thread_id, null); printf("message read parent- %s", buffer); return 0; }
if code , synchronization more complex, lock idea. might want add structure (or, equivalently, add buffer size structure).