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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:09:40+00:00 2026-05-29T06:09:40+00:00

Does anyone know if when creating a mutex, it’s a must to initialize it

  • 0

Does anyone know if when creating a mutex, it’s a must to initialize it or can i lock it directly without calling pthread_mutex_init?

I have done a sample application that simulates a deadlock just to make sure the mutex work and have declared 2 mutexes(to create the deadlock) in the following way:

static pthread_mutex_t fastmutex1 = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t fastmutex2 = PTHREAD_MUTEX_INITIALIZER; 

The deadlock perfectly works which makes sense since it’s initialized with some defaults.
On the other hand when doing the exact same thing with this:

static pthread_mutex_t fastmutex1;
static pthread_mutex_t fastmutex2;

I expected that not to work but the deadlock appeared in the exact same way as the previous example.

By the way I am running that on Linux kernel 2.6.18

Thx for 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-05-29T06:09:41+00:00Added an answer on May 29, 2026 at 6:09 am

    On my Debian/Sid/AMD64 system, /usr/include/pthread.h contains

     # define PTHREAD_MUTEX_INITIALIZER \
       { { 0, 0, 0, 0, 0, 0, { 0, 0 } } }
    

    This means that (on my system) a pthread_mutex_t is valuably initialized to all zeros. And a static variable is initialized (in C) to all zeros, which happens to be the same at runtime (and explains the behavior you’ve got).

    However, there is no guarantee that PTHREAD_MUTEX_INITIALIZER will stay the same, or that is is all zeros on other systems. So you better explicitly initialize a static pthread_mutex_t variable with it.

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

Sidebar

Related Questions

pthread_create returns the value 251 without creating the thread. Does anyone know what the
Just a quick question, does anyone know if there are restrictions on creating Performance
I'm wanting to play around with creating an LR parser generators. Does anyone know
Does anyone know of any existing packages or libraries that can be used to
Does anyone know where online copies of the old The Perl Journal articles can
Does anyone know of a good screencast, walkthrough or tutorial for creating WCF Restful
Does anyone know the logic behind creating a recursive function to generate all combinations
Does anyone know the recommended way of creating a FaceBook 'likes' button using the
Does anyone know of a good guide for creating a project with the new
Does anyone know the syntax for creating a custom HtmlHelperextension method which behaves like..

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.