The common usage of the nameless semaphore is that the variable to be protected is placed in the critical region formed between the sem_wait and the sem_post, so that the variable is//protected, for example:
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int number;//protected global variable
sem_t sem_id;
void* thread_one_fun (void *arg)
{
sem_wait (&sem_id);
printf ("Thread_one has the semaphore\n");
number++;
printf ("number =%d\n", number);
Sem_post (&sem_id);
}
void* thread_two_fun (void *arg)
{
sem_wait (&sem_id);
printf ("Thread_two has the semaphore \ n");
number--;
printf ("number =%d\n", number);
Sem_post (&sem_id);
}
int main (int argc,char *argv[])
{
Number = 1;
pthread_t id1, Id2;
sem_init (&sem_id, 0, 1);
pthread_create (&id1,null,thread_one_fun, NULL);
pthread_create (&id2,null,thread_two_fun, NULL);
Pthread_join (id1,null);
Pthread_join (id2,null);
Sem_destroy (&sem_id);
printf ("main,,, \ n");
return 0;
}
///above the routine, exactly which line enters upgradeable applies to the semaphore resource, which is random. If you want a specific order, you can use 2 semaphores//to achieve it. For example, the following routine is thread 1 execution, and thread 2 continues until the end.
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int number;//protected global variable
sem_t sem_id1, Sem_id2;
void* thread_one_fun (void *arg)
{
sem_wait (&SEM_ID1);
printf ("Thread_one has the semaphore\n");
number++;
printf ("number =%d\n", number);
Sem_post (&SEM_ID2);
}
void* thread_two_fun (void *arg)
{
sem_wait (&SEM_ID2);
printf ("Thread_two has the semaphore \ n");
number--;
printf ("number =%d\n", number);
Sem_post (&SEM_ID1);
}
int main (int argc,char *argv[])
{
Number = 1;
pthread_t id1, Id2;
sem_init (&sem_id1, 0, 1);//Idle
sem_init (&sem_id2, 0, 0);//Busy
pthread_create (&id1,null,thread_one_fun, NULL);
pthread_create (&id2,null,thread_two_fun, NULL);
Pthread_join (id1,null);
Pthread_join (id2,null);
Sem_destroy (&SEM_ID1);
Sem_destroy (&SEM_ID2);
printf ("main,,, \ n");
return 0;
}
/ * Build two threads, each of these two threads increments its own integer variable i from 1 to 100, and controls the incremental process through the semaphore, that is, the difference between these two integer variables cannot exceed 5. */
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#define MAX
sem_t sem1,sem2;
void* th_fn1 (void* Arg)
{
int i;
For (i = 0; i < MAX; ++i)
{
sem_wait (&sem1);
printf ("Numberin Thread1 is%d\n", i);
Sem_post (&SEM2);
}
Pthread_exit ((void*) "thread1exit\n");
}
void* th_fn2 (void* Arg)
{
int i;
For (i = 0; i < MAX; ++i)
{sem_wait (&sem2);
printf ("number in Thread2 is%d\n", i);
Sem_post (&sem1);
}
Pthread_exit ((void*) "thread2exit\n");
}
int main (void)
{
Void*tret;
Sem_init (&sem1,0,5);
Sem_init (&sem2,0,5);
pthread_t Tid1,tid2;
pthread_create (&tid1,null,th_fn1,null);
pthread_create (&tid2,null,th_fn2,null);
Pthread_join (Tid1,&tret);
Pthread_join (Tid2,&tret);
Sem_destroy (&sem1);
Sem_destroy (&SEM2);
return 0;
}
The unknown semaphore is synchronized between multiple threads