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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:17:44+00:00 2026-05-14T03:17:44+00:00

gcc 4.4.2 c89 I have a function that has to run (config_relays). It make

  • 0

gcc 4.4.2 c89

I have a function that has to run (config_relays). It make a call to a API function called set_relay, then the code has to wait before continuing until the event for set_relay event has completed. The set_relay is any Async call.

i.e.

void run_processes()
{
    switch()
    {
        case EV_RELAY_SET:
        break;
    }
}


void config_relays()
{
    set_relay();

    /* Wait until EV_RELAY_SET has fired */
    /* Cannot do init_relay until set_relay event has fired - has to block here */
    init_relay();
}

I guess I could put the init_relay() in the switch. However, that event is used for other things and not just for initializing the relay. I would really like to handle everything in the config_relays function.

In C# you can do this by using autoreset. Does C have anything like that.

Many thanks for any advice,

  • 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-14T03:17:45+00:00Added an answer on May 14, 2026 at 3:17 am

    As Anders wrote, conditional wait is the solution. In the POSIX Thread API you use pthread_cond_wait together with a mutex. It is quite easy, the following pattern works:

    int ready_flag = 0;
    pthread_mutex_t ready_mutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER;
    
    void wait_ready()
    {
      pthread_mutex_lock(&ready_mutex);
      while(!ready_flag) {
        pthread_cond_wait(&ready_cond, &ready_mutex);
      }
      pthread_mutex_unlock(&ready_mutex);
    }
    
    
    void set_ready(int ready)
    {
      pthread_mutex_lock(&ready_mutex);
      ready_flag = ready;
    
      pthread_cond_signal(&ready_cond);
    // or using pthread_cond_broadcast(&ready_cond);
    
      pthread_mutex_unlock(&ready_mutex);
    
    }
    

    The difference between pthread_cond_signal and pthread_cond_broadcast is that if more than one thread waits for the flag to be set, pthread_cond_signal only releases one thread but broadcast releases all threads.

    Not that the while loop created around your condition is up to you, you can test multiple conditions or do whatever you want. The code pattern ensures that your tests are performed on protected variables so that race conditions can never cause problems for example

    while(resource_a_busy && resource_b_busy) ...

    Is a typical problem where both resource states must be protected by a mutex.

    The cond_wait can be removed from the loop, but then it would translate the wait_ready to a polling loop which consumes CPU, the pthread_wait_cond does not consume any CPU.

    There are porting libraries that provides a Win32 like API on top of pthreads as well as libraries that gives a phread like API on top of Win32 event API, the later is called [Pthreads-w32] 1

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

Sidebar

Related Questions

gcc 4.4.2 c89 I have this code snippet that I have to repeat in
gcc 4.4.4 c89 I have the following code and 2 structures that have to
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
gcc 4.4.3 c89 I have the following code as a sample of what I
gcc 4.4.4 c89 I have seen this in some code I am maintaining. #define
gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char
gcc 4.4.1 c89 I have the following code: static enum states { ACTIVE, RUNNING,
gcc 4.4.3 c89 I have the following source code. And getting a stack dump
gcc 4.4.4 c89 I have the following file that contains name, age, and gender.
gcc 4.4.1 I am maintaining someone's code and I have come across something that

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.