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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:45:39+00:00 2026-06-18T15:45:39+00:00

gcc 4.7.2 c89 Hello, I am just wondering about applying a mutex lock for

  • 0
gcc 4.7.2
c89

Hello,

I am just wondering about applying a mutex lock for the following code snippet.

Is there any rules that you should following, as I don’t want to lock as this would block other threads in this function. As this would really slow things down.

I am compiling with the following CFLAGS:

-Wall -Wextra -g -m32 -O2 -D_DEBUG -D_THREAD_SAFE -D_REENTRANT -D_LARGEFILE64_SOURCE

Code snippet

static void* APR_THREAD_FUNC timeout_duration(apr_thread_t *thd, void *data)
{
apr_status_t rv = 0;
channel_t *channel = NULL;

/*
  APPLY LOCK HERE
*/
channel = (channel_t*)data;

/* simulate some work */
apr_sleep(5000000);

LOG_INFO("Channel id [ %d ] Channel name [ %s ] Delay time [ %d ]",
         channel->id,
         channel->name,
         (apr_int32_t)channel->delay_time);

/*
  UNLOCK HERE
*/

return NULL;
}

I pass channel as the data that is passed into the entry function. However, isn’t this just a copy so I don’t really need to worry about it?

  • 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-18T15:45:40+00:00Added an answer on June 18, 2026 at 3:45 pm

    The rules are:

    1) Locks protect data and not code. When data is protected by a lock, code that accesses that data must acquire the data’s lock.

    2) Locks should be acquired as late as possible and released as early as possible. This can include shifting work from inside the critical section to outside the critical section.

    3) Data that is only read (and not modified) doesn’t need a lock. This includes things like "Channel id [ %d ] ..." format strings (which should be treated as constant).

    4) Data that can only be accessed by one thread doesn’t need a lock. This includes things like function parameters and local variables.

    5) Finer grained locking is better than coarse grained locking. For example, rather than having one large data structure with one lock, often you can split that large data structure into many smaller structures with many locks.

    6) If any code needs more than one lock at a time, you need to define a “locking order”. For example, if one thread acquires lock A then lock B, does some work then releases the locks; and if another thread acquires lock B then lock A, does some work then release the locks; then you can get deadlock (each thread has one lock but needs both to continue). Defining a “locking order” (e.g. saying that lock A must be acquired before lock B) prevents this sort of bug.

    For your code, the first few lines don’t need a lock at all because they only access function parameters and local variables (rule 4). The data pointed to by void *data may or may not need a lock depending on what it is – e.g. if each thread has its own separate data (rule 4), or if that data is only read (rule 3), then no lock is needed. For the LOG_INFO() function no additional lock is needed (excluding the void *data lock if it exists) in the code you posted, but it may have its own internal lock (e.g. to protect a shared log).

    For an example of rule 2, if the LOCK_INFO takes a little while your code could do something like this to release the first lock earlier:

    temp_ID = channel->id;
    temp_name = strdup(channel->name);  // Should check for NULL!
    temp_delay = channel->delay_time;
    
    /*
      UNLOCK HERE
    */
    
    LOG_INFO("Channel id [ %d ] Channel name [ %s ] Delay time [ %d ]",
             temp_ID,
             temp_name,
             temp_delay);
    free(temp_name);
    

    Also note that if LOCK_INFO() uses a lock, releasing the first lock earlier will also help with rule 6.

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

Sidebar

Related Questions

gcc 4.4.4 c89 I am just wondering is there any standard that should be
gcc (GCC) 4.6.3 c89 Hello, I am just wondering if this is the best
gcc 4.7.2 c89 Hello, I am wondering does any one know of any tutorials
gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 c89 Just wondering is there a better way to do
gcc (GCC) 4.6.3 c89 apache runtime portable libraries Hello, Just a simple question I
gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) c89 Hello, I am wondering if I
gcc 4.4.3 c89 I am just getting started with log4c. However, there is very
gcc 4.7.2 c89 Hello, I am getting the following warning: pointer/integer type mismatch in
gcc 4.6.2 c89 I am just wondering if this is a good way 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.