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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:21:19+00:00 2026-06-02T00:21:19+00:00

I have a big problem, I can’t figure out why mutexes in C don’t

  • 0

I have a big problem, I can’t figure out why mutexes in C don’t work as I expect.
This is my code:

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

pthread_t mythread;
pthread_mutex_t mymutex;

void *anotherFunc(void*)
{
    pthread_mutex_lock(&mymutex);

    for(int i = 0; i < 100; i++)
        printf("anotherFunc\n");

    pthread_mutex_unlock(&mymutex);

    pthread_exit(NULL);
}

void *func(void*)
{
    pthread_mutex_lock(&mymutex);

    for(int i = 0; i < 100; i++)
        printf("func\n");

    pthread_mutex_unlock(&mymutex);

    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    pthread_mutex_init(&mymutex, NULL);

    pthread_create(&mythread, NULL, func, NULL);
    pthread_create(&mythread, NULL, anotherFunc, NULL);

    pthread_mutex_destroy(&mymutex);

    pthread_exit(NULL);
    return EXIT_SUCCESS;
}

What I expect to happen is the program to print first 100 “func” messages and then 100 “anotherFunc” messages. What I expect is execution to reach func and lock the mutex. When the execution reaches anotherFunc, I expect to wait until func unlocks the mutex. But I get interfered messages like

func
func
func
anotherFunc
anotherFunc
anotherFunc
func
anotherFunc

I don’t understand how this thing works. Please help!

  • 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-02T00:21:21+00:00Added an answer on June 2, 2026 at 12:21 am
    pthread_create(&mythread, NULL, func, NULL);
    pthread_create(&mythread, NULL, anotherFunc, NULL);
    
    pthread_mutex_destroy(&mymutex);
    

    You’re destroying the mutex before the threads are done with it, so all bets are off. You’ll probably want to pthread_join the 2 threads before destroying it.

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

Sidebar

Related Questions

I have a big problem with this code and I have no idea how
Hey, I have a big problem with my container-sketch. How can I realize this
I have a (big) problem that all of you that work with Webforms might
I have a big problem with very simple code. I need to get a
I've created a form with code and I have a big problem. Every element
I have a big problem since a few days that I can't solve. First
I have a big problem to resolve and I don't know how to resolve
I have a very big problem and can't seem to find anybody else on
I have a big problem with my web site ( you can see here
I have a big problem opening http://localhost/ on Windows 7 (beta). I installed this

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.