0

I have a doubt regarding synchronized threads. See the below code

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/types.h>
#include<semaphore.h>

int a=0;
sem_t s1, s2;

void* t1handler(void *data)
{
    while(1){
        sem_wait(&s1);
        a++;
        printf("incr %d\n",a);
        sem_post(&s2);
    }
}

void* t2handler(void* data)
{
    while(1){
        sem_wait(&s2);
        a--;
        printf("decr %d\n",a);
        sem_post(&s1);
    }
}

int main(){
    pthread_t t1,t2;

    sem_init(&s1,0,1);
    sem_init(&s2,0,0);

    pthread_create(&t1,NULL,t1handler,NULL);
    pthread_create(&t2,NULL,t2handler,NULL);

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    return 0;
}

May be not a good example, but here thread2 waits for thread1 to complete and vice versa to synchronize. Here what is the use of threads when both are not executing simultaneously?.

Any examples where threads can be used for synchronization?

Thanks in advance.

Ramana Reddy
  • 369
  • 1
  • 8
  • 28
  • 1
    Errr... you wrote the code, you tell *us* what the point of it is. – Kerrek SB Sep 15 '16 at 12:49
  • @KerrekSB This is just an example. The question is where can I use synchronized threads – Ramana Reddy Sep 15 '16 at 12:53
  • 1
    With your simple example: What if thread 1 does the increase (`a++`) then the thread is preempted and thread 2 does a decrease, then thread 2 is preempted and thread 1 continues, thinking that the value of `a` is something its not. With "synchronization" then the value of `a` will not change unexpectedly in any of the two threads. – Some programmer dude Sep 15 '16 at 12:53
  • @JoachimPileborg Yes, but what is the need of using `threads` here which can be done using two `functions` one called after the other. The question I posted is just an example. Can you tell me in a generic way where to use `threads` with `synchronization` instead of using two `functions` called one after the other – Ramana Reddy Sep 15 '16 at 12:57
  • Basically whenever you access shared resources or variables... But it would be better to read a book instead of asking this on SO. – alain Sep 15 '16 at 13:03
  • Well this *is* a very simplified example you're showing us, so of course it could be replaced by a simple single function without threads. You have to think of bigger things, where the synchronized parts are only smaller parts of some bigger functions and that does something more important. The thing is: If you have shared data, you should synchronize the access to it. End of story. – Some programmer dude Sep 15 '16 at 13:15

1 Answers1

1

Your example is not very explicit. I have changed it slightly:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <semaphore.h>

int a=0;
sem_t s1, s2;

void* t1handler(void *data)
{
    while(1){
        sem_wait(&s1);
        printf("send 1000$ to %d\n",a);
        sem_post(&s2);
    }
}

void* t2handler(void* data)
{
    while(1){
        sem_wait(&s2);
        a++;
        printf("client is now #%d\n",a);
        sem_post(&s1);
    }
}

int main(){
    pthread_t t1,t2;

    sem_init(&s1,0,1);
    sem_init(&s2,0,0);

    pthread_create(&t1,NULL,t1handler,NULL);
    pthread_create(&t2,NULL,t2handler,NULL);

    pthread_join(t1,NULL);
    pthread_join(t2,NULL);
    return 0;
}

If the threads were not sync then one client could get 2000$ or 0$.

In the real world a possible use of such threads is when one is getting input (for example a movie title) and the other is producing output (streaming the movie).

Dominique Lorre
  • 1,168
  • 1
  • 10
  • 19