#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include < pthread.h> #include <semaphore.h>void *thread_function (void *arg);p thread_mutex_t Work_mutex; #define WORK_ SIZE 1024char work_area[work_size];int time_to_exit = 0;int Main () {int res; pthread_t A_thread; void *thread_result; res = Pthread_mutex_init (&work_mutex, NULL); if (0! = res) {perror ("Mutex initialization Failed"); Exit (Exit_failure); } res = pthread_create (&a_thread, NULL, thread_function, NULL); if (0! = res) {perror ("Thread creation failed"); Exit (Exit_failure); } pthread_mutex_lock (&work_mutex); printf ("Input some text. Enter ' End ' to finish\n "); while (0 = = time_to_exit) {fgets (Work_area, Work_size, stdin); Pthread_mutex_unlock (&work_mutex); while (1) {pthread_mutex_lock (&work_mutex); if ('% '! = work_area[0]) {pthread_mutex_unlock (&work_mutex); Sleep (1); } else break; }} pthread_mutex_unlock (&work_mutex); printf ("\nwaiting for Thread to finish...\n"); res = Pthread_join (A_thread, &thread_result); if (0! = res) {perror ("Thread join failed"); Exit (Exit_failure); } printf ("Thread joined\n"); Pthread_mutex_destroy (&work_mutex); Exit (exit_success);} void *thread_function (void *arg) {sleep (1); Pthread_mutex_lock (&work_mutex); while (0! = strncmp ("End", Work_area, 3)) {printf ("You input%d characters\n", strlen (Work_area)-1); Work_area[0] = ' + '; Pthread_mutex_unlock (&work_mutex); Sleep (1); Pthread_mutex_lock (&work_mutex); while (' n ' = = Work_area[0]) {pthread_mutex_unlock (&work_mutex); Sleep (1); Pthread_mutex_lock (&work_mutex); }} time_to_exit = 1; Work_area[0] = ' + '; Pthread_mutex_unlock (&work_mutex); Pthread_exit (0);}
The new thread first attempts to lock the mutex. If it is already locked, the call will be blocked until it is released. Once access is granted, first check whether there is a request to launch the program. If so, set the Time_to_exit variable, then set the first character of the work to \ s, then exit.
If you do not want to exit, count the number of characters and then set the first character in the Work_area array to null. We have used the method of setting the first character to NULL to inform the read input thread that we have completed the character count. It then unlocks the mutex and waits for the main thread to continue executing. We will periodically try to lock the mutex, if the lock succeeds, check whether the main thread and the character sent to be processed. If not, the unlock mutex continues to wait, and if so, counts the number of characters and enters the loop again.
That is, in addition to the first input, the other input to count the characters, the new thread has been at the same time on the mutex lock unlock, to determine whether you have entered the completion.
The code for the main thread is similar to the new thread, first locking the workspace, reading the text into it, and unlocking it to allow other threads to access it and count the number of characters. Periodically locks the mutex and checks whether the number of characters has been counted. If you still need to wait, release the mutex.
This method of finding results through the theory is not a good way to become. In the actual change, we should use the semaphore as much as possible to avoid this situation.
Code Run Result:
Examples of basic usages of mutexes in POSIX threads