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 8769183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:16:15+00:00 2026-06-13T17:16:15+00:00

The following code should output NITER * 2, but it seems like still no

  • 0

The following code should output NITER * 2, but it seems like still no mutex working, any idea?

and why clang gives me the following warning:

semaphore-example-add-semaphore.c:24:1: warning: control reaches end of non-void
      function [-Wreturn-type]
}
^
1 warning generated.

code:

#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <semaphore.h>

#define NITER 1000000

int count = 0;

sem_t mutex;


void * ThreadAdd(void * a)
{
    int i, tmp;
    for(i = 0; i < NITER; i++)
    {
        sem_wait(&mutex);
        tmp = count;
        tmp = tmp + 1;
        count = tmp;
        sem_post(&mutex);
    }
}

int main(int argc, char * argv[])
{
    pthread_t tid1, tid2;
    sem_init(&mutex, 0, 1);
    if(pthread_create(&tid1, NULL, ThreadAdd, NULL))
    {
        printf("\n ERROR create thread 1");
        exit(1);
    }
    if(pthread_create(&tid2, NULL, ThreadAdd, NULL))
    {
        printf("\n ERROR create thread 2");
        exit(1);
    }
    if(pthread_join(tid1, NULL))
    {
        printf("\n error joining thread");
        exit(1);
    }
    if(pthread_join(tid2, NULL))
    {
        printf("\n ERROR joining thread");
        exit(1);
    }
    if(count < 2 * NITER)
        printf("\n BOOM! count is [%d], should be %d\n", count, 2 * NITER);
    else
        printf("\n OK! count is [%d]\n", count);
    pthread_exit(NULL);
}
  • 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-06-13T17:16:17+00:00Added an answer on June 13, 2026 at 5:16 pm

    The clang error is because ThreadAdd is declared as void * and doesn’t return anything. Just return 0.

    One issue is that sem_wait and sem_post can fail. In that case, they return -1 and you need to check errno for the cause. Your code looked okay to me so I tried it on two machines:
    – SE Linux, worked just fine
    – Mac, sem_wait failed.
    So the direct problem is you aren’t checking the return value.

    I found another post that stated sem_init is NOT supported on OS X (yeah Apple) but that sem_open is supported. I tried your code using sem_open and it worked. Nothing in the docs that I could see gave any hint this was the case. I’d link to the other post but I lost the address in a machine change…

    I see Jack also posted this…

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

Sidebar

Related Questions

I have the following PHP code, but it's not working. I don't see any
I have the following code but it's output is not what I expected: public
According the the MSDN documentation, the following code should output '5' to the console
I am sure that the following code should not compile. But, in g++, it
I feel really stupid right now. The following code should output 'If you can
The output from the following code is incorrect. It should only output the leaves
The following code should show a certain track in iTunes: NSString* iTunesPath = [[NSWorkspace
The following code should pop up an alert box whenever I select a file
The following code should display the id out of the JSON-object, which I got
The following code should exhibit intended functionality: <head> <style> a:active { color:teal; } a:hover

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.