Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6170445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:02:13+00:00 2026-05-23T23:02:13+00:00

#include<pthread.h> #include<stdio.h> #include<semaphore.h> void func(); int a; int main() { pthread_t thread1; sem_t semaphore1;

  • 0
          #include<pthread.h>
          #include<stdio.h>
          #include<semaphore.h>
              void func();
              int a;
              int main()
              {  

                 pthread_t thread1;
                 sem_t semaphore1;
                 sem_init(&semaphore1,0,1);
                 pthread_create(&thread1,NULL,(void *)func,NULL);
                 sem_wait (&semaphore1);
                 a=62;
                 printf("%d",a); \\ as i found on google 
                 sleep(2);   \\ i believe a value should be sticked to 62
                 sleep(1);    \\ but output shows different why?
                 printf("%d",a);
                 sem_post(&semaphore1);
              } 
            void func()
             {  
              a=45;
              sleep(1);
              a=32;
              a=75;
           printf("hello");
             }

When i Googled it.I found sem_wait locks the global variable so that no other thread access the variable.

But when i tried these code the output is

           62 
           hello 
           75.

The a value got changed but note that the printf(“%d”,a) is under the sem_wait ,Whats wrong with my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T23:02:14+00:00Added an answer on May 23, 2026 at 11:02 pm

    Semaphores offer only advisory locking. They don’t know about variables and such, they lock regions of your code. They don’t enforce anything so you must call wait and post yourself.

    Here is what wait and post really mean when used in your example.

    sem_wait (&semaphore1); /* AKA "may I enter this region" */
    
    sem_post(&semaphore1); /* AKA "I am done with this region. */
    

    The way I see it, main asks for permission before entering. func doesn’t ask for permission before modifying a.

    So func should wait and post.

    void func()
    {
        sem_wait (&semaphore1);
        a=45;
        sleep(1);
        a=32;
        a=75;
        printf("hello");
        sem_post (&semaphore1);
    }
    

    Of course, for this sempahore1 should be globally accessible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <errno.h> #include <pthread.h> pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; void *func(void *arg) {
#include <pthread.h> #include <stdio.h> void* printHello (void* threadId) { pthread_t *my_tid = (pthread_t *)threadId;
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
#include <stdio.h> #include <pthread.h> #include <time.h> #include <stdlib.h> typedef struct pr_struct{ int owner; int
see i have code like #include<stdio.h> #include<pthread.h> #include<string.h> void* thread_function(void) { printf (This is
I have the following code: #include <stdlib.h> #include <stdio.h> #include <pthread.h> #define NUM_THREADS 100
#include <iostream> using namespace std; int main() { double u = 0; double w
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include<iostream> class name { public: int a; name():a(0){}; }; void add(name * pname) {
I study this code from some book: #include <pthread.h> #include <stdio.h> /* Parameters to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.