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

  • Home
  • SEARCH
  • 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 8103285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:30:03+00:00 2026-06-05T23:30:03+00:00

I am working on a multi-thread program, and have a question about where to

  • 0

I am working on a multi-thread program, and have a question about where to define the mutex.

Relevant information: the program has a main.c where we determinate a specific action according to the user input. main calls a master_function which is located in a file named master.c. In the master.c file we create the N threads along some other actions (not relevant). The threads call a function named son_threads which is located in a son.c file and they need to use a mutex when they enter enter a critical region (editing several global variable to prevent race condition). Another file I have is a type.h where I define several global variables I need to use.

The declaration of mutex is:

  pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;

So I tried to define the mutex in my type.h so it’s visible to the son.c files. When I try to compile it gives me error. Which is correct since I am defining that mutex in several files.

But I am pretty sure I cant define the mutex in the son.c file because each time I create that thread, the mutex will be initialized to its default setting, not allowing me to use it properly. Not sure about this.

The mutex has to be a global variable, where the N threads have access to it. So where am I supposed to put it?

I dont know if I am explaining myself right. Trying my best.

  • 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-05T23:30:05+00:00Added an answer on June 5, 2026 at 11:30 pm

    Just declare your variable in the .h file

    extern pthread_mutex_t mutex1;
    

    and keep the definition with the initialization in the C file. This is as it is meant by the C standard(s).

    For POSIX, the initialization of the mutex with static storage is really important. So that definition can’t live in a .h file.

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

Sidebar

Related Questions

I have made a multi-threading program. In it, the main thread starts 10 threads
Good evening fellow stackers, I have a mutex and threaded related question about //
I have a multi threaded program in C that was working well but was
i'm working with a multi-threaded program (using pthreads) that currently create a background thread
I am working with a multi-thread program. First I redirect my stdout to a
I have a multi-threaded Cocoa app that processes images. The program has a progress
I'm working on a multi-threaded Silverlight application. The application has two threads: Main/UI and
I’m working on a multi dimensions array but i have a problem Imagine I
I've a multi-threaded app where main thread initiate two threads: MakeRequest Thread QueueListener Thread
I have a multi-threaded Delphi 6 Pro application that I am currently working on

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.