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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:09:10+00:00 2026-06-18T08:09:10+00:00

I am writing an network application. and have some problem regarding thread race condition.

  • 0

I am writing an network application.
and have some problem regarding thread race condition.

“cd” is a socket descriptor.
one of my thread retrieves socket descriptor
and send some data through the socket.

lets say map_sd returns 5.
however another thread might close the socket 5 and
reassign another. which will destroy the logic of program.

                // wait until there is valid descriptor mapping
                while( !(cd = map_sd( sd )) ){
                    sleep(1);
                }                   

                // forward PAYLOAD header
                if( send(cd, &payload, sizeof(PAYLOAD), MSG_NOSIGNAL) < 0 ){    
                    printf("send fail 813\n");
                }

what I want is to make the code above “atomic”
how can I do this when I am using pthread library in linux??

thank you in advance.

  • 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-18T08:09:11+00:00Added an answer on June 18, 2026 at 8:09 am

    You need a condition variable and a mutex:

    pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
    pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
    

    In the thread passing cd:

    pthread_mutex_lock(&lock);
    /* here you pass cd through a data structure or whatever */
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&lock);
    

    In the thread waiting for cd:

    pthread_mutex_lock(&lock);
    if (pthread_cond_wait(&cond, &lock) != 0) {
        /* handle error */
    }
    /* here you can acquire cd */
    pthread_mutex_unlock(&lock);
    

    That should be it – you use a condition variable and a lock for exclusive access and notifying the other thread that a resource is now available. pthread_cond_wait() releases the lock to wait and will reacquire it once it has been notified by the other thread with pthread_cond_signal().

    Edit: formatting.

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

Sidebar

Related Questions

Hey, I'm writing a network application, in which I read packets of some custom
I have a Java application that is writing to the network. It is writing
I have an application which uses some external library for analytics. Problem is that
I'm writing an application that manipulates some sort of social network data, so the
I am working on writing a network application in C++ on the Linux platform
I am writing an Android application which can enable and disable the Network Data
I am writing an iPhone app – a client for some social network. The
I'm writing in .NET a windows service that, at windows startup, executes some network
I'm writing an application which can copy files on a network share. Usually the
I am having an application where I have to send several small data per

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.