The difference between a semaphore and a mutex (mutex) is that a mutex allows only one thread to enter the critical section, and the semaphore allows multiple threads to enter the critical section at the same time.
Not much to explain, to use the semaphore synchronization, need to include the header file semaphore.h.
The main functions used are:
int sem_init(sem_t *sem, int pshared, unsigned int value);
, which sem
is the semaphore to initialize, pshared
indicating whether this semaphore is shared between processes or between threads, and value is the initial value of the semaphore.
int sem_destroy(sem_t *sem);
, which sem
is the amount of semaphore to be destroyed. Only sem_init
the initialized semaphore can be used for sem_destroy
destruction.
int sem_wait(sem_t *sem);
Wait for the semaphore, if the semaphore value is greater than 0, the value of the semaphore is reduced by 1 and returned immediately. If the value of the semaphore is 0, the thread is blocked. Equivalent to P operation. Successful return 0, Failure returns-1.
int sem_post(sem_t *sem);
Release the semaphore and let the value of the semaphore increase by 1. Equivalent to V operation.
The user enters any character from the terminal and then the count is displayed, and the end is entered.
Using multithreaded implementations: The main thread gets user input and determines whether to exit, child thread count
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
Char buf[100]={0};
int flag;
sem_t sem;
A subroutine that counts the number of characters in a buf and prints
void *func (Void*arg)
{
A child thread should have a loop first
Blocking in the loop while waiting for the main thread to activate, the child thread is activated and then gets the characters in the BUF.
length, then print;
Sem_wait (&sem);
while (flag==0)
{
printf ("Length:%d.\n", strlen (BUF));
memset (buf, 0, sizeof (BUF));
Sem_wait (&sem);
}
Pthread_exit (NULL);
}
int main (void)
{
int ret=-1;
pthread_t th;
Sem_init (&sem,0,0);
Ret=pthread_create (&th,null,func,null);
if (ret! = 0)
{
printf ("Pthread_create error.\n");
return-1;
}
printf ("Enter a string to end with a carriage return. \ n");
while (scanf ("%s", buf))
{
To compare the user input is not end, if yes then exit, if not then continue
if (!strncmp (buf, "End", 3))
{
printf ("Input string:%s", buf);
Flag==1;
Sem_post (&sem);
Break
}
The main thread receives a string of user income and confirms that it is not the end
Just send a signal to activate the thread count.
Child threads are blocked and the main thread can be activated, which is the thread synchronization problem.
The semaphore can be used to implement this thread synchronization
Sem_post (&sem);
}
/*
Reclaim Child Threads
printf ("Waiting to reclaim sub-threads \ n");
ret = pthread_join (th, NULL);
if (ret! = 0)
{
printf ("Pthread_join error.\n");
Exit (-1);
}
printf ("Child thread Reclaim succeeded \ n");
Sem_destroy (&sem);
*/
return 0;
}
Signal volume for thread synchronization (sem_init,sem_post,sem_wait)