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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:52:11+00:00 2026-06-19T02:52:11+00:00

I have a c program running in Linux kernel 2.6.18-194, server has 1 cpu

  • 0

I have a c program running in Linux kernel 2.6.18-194, server has 1 cpu socket with 6 cores with hyper-threading , thread1 receive data and then thread2 and thread3 pass the data thread1 received to another process , after both thread2 and thread3 successfully finish passing data , thread1 will receive data again !!

The floowing is thread1 source :

        DoGetDataFromSocket() ;
        iGlbBOOKReadDone = 0 ;
        iGlbPOSIReadDone = 0 ;
        sem_post(sembook) ;
        sem_post(semposi) ;
        sem_wait(semfinished) ;

The following is thread2 and thread3 source :

    if(bThisThreadIsBook==1)
        sem_wait(sembook) ;
    else
        sem_wait(semposi) ;

    DoPassDatatoAnotherProcess() ;

    if(bThisThreadIsBook==1)
    {
        __sync_add_and_fetch(&iGlbBOOKReadDone,1) ;
    }
    else
    {
        __sync_add_and_fetch(&iGlbPOSIReadDone,1) ;
    }

    Pthread_mutex_lock(&DoneMutex) ;
    if(  (iGlbBOOKReadDone == 1) && (iGlbPOSIReadDone == 1) )
        sem_post(semfinished) ;
    Pthread_mutex_unlock(&DoneMutex) ;

It works fine to me , I try to remove mutex_lock DoneMutex in thread2 and thread3 , it still works fine , what I am curious is , if thread2 is doing __sync_add_and_fetch(&iGlbBOOKReadDone,1) , and thread3 is doing __sync_add_and_fetch(&iGlbPOSIReadDone,1)
at exact the same time , then both thread will take if( (iGlbBOOKReadDone == 1) && (iGlbPOSIReadDone == 1) ) to false , and sem_post(semfinished) would never be called ,
But I do a lot of pressure tests ,this never happen!! Is it related with __sync_add_and_fetch function ?

  • 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-19T02:52:12+00:00Added an answer on June 19, 2026 at 2:52 am

    As mentioned in my comment, regardless of whether you use the mutex or not you have a race condition where the semfinished semphore can be posted twice, which means that thread1 might not allow the threads to finish in a future round.

    It’s easy to see that both thread2 and thread3 can simultaneously ‘reach’ the blank line just before the pthread_mutex_lock() call. If that happens, then both threads will call sem_post(semfinished).

    To avoid this situation and to make the code easier to reason about, making it so you can be certain that exactly one of thread2 and thread3 will callsem_post(semfinished)`, you might want to consider doing something like the following:

    • thread1:

      DoGetDataFromSocket() ;
      threadDoneCount = 0;
      sem_post(sembook) ;
      sem_post(semposi) ;
      sem_wait(semfinished) ;
      
    • thread2 and thread3:

      if(bThisThreadIsBook==1)
          sem_wait(sembook) ;
      else
          sem_wait(semposi) ;
      
      DoPassDatatoAnotherProcess() ;
      
      int doneCount = __sync_add_and_fetch(&threadDoneCount,1) ;
      
      if (doneCount == 2)
          sem_post(semfinished) ;
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a java program running on a linux server. I need to send
I have a http server program running on linux host say B that listens
I have a C++ program/Linux, which within 2-3 seconds of running starts spitting error
I have a question about debugging a running C++ program in Linux. If a
I have a program in Octave that has a loop - running a function
I have a program in C++, using the standard socket API, running on Ubuntu
I have a C++ program running under linux. Is it possible to track its
I am cross compiling linux kernel. I have the following program which compiles perfectly
I have a program written in C#, running on Linux using Mono. The program
I have a program running on Linux and I need to determine how it

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.