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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:15:41+00:00 2026-06-04T13:15:41+00:00

This is an interview question, the interview has been done. How to make thread

  • 0

This is an interview question, the interview has been done.

How to make thread synchronization without using mutex, semorphore, spinLock and futex ?

Given 5 threads, how to make 4 of them wait for a signal from the left thread at the same point ?
it means that when all threads (1,2,3,4) execute at a point in their thread function, they stop and wait for
signal from thread 5 send a signal otherwise they will not proceed.

My idea:

Use global bool variable as a flag, if thread 5 does not set it true, all other threads wait at one point and also set their
flag variable true. After the thread 5 find all threads’ flag variables are true, it will set it flag var true.

It is a busy-wait.

Any better ideas ?

Thanks

 the pseudo code:
 bool globalflag = false; 
 bool a[10] = {false} ;  
 int main()
 {
  for (int i = 0 ; i < 10; i++)
  pthread_create( threadfunc, i ) ; 

      while(1)
      {
         bool b = true; 
         for (int i = 0 ; i < 10 ; i++)
         {  
              b = a[i] & b ; 
         }
         if (b) break; 
    }
  }
  void threadfunc(i)
  {
   a[i] = true; 
   while(!globalflag); 
  }
  • 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-04T13:15:42+00:00Added an answer on June 4, 2026 at 1:15 pm

    Start with an empty linked list of waiting threads. The head should be set to 0.

    Use CAS, compare and swap, to insert a thread at the head of the list of waiters. If the head =-1, then do not insert or wait. You can safely use CAS to insert items at the head of a linked list if you do it right.

    After being inserted, the waiting thread should wait on SIGUSR1. Use sigwait() to do this.

    When ready, the signaling thread uses CAS to set the head of wait list to -1. This prevents any more threads from adding themselves to the wait list. Then the signaling thread iterates the threads in the wait list and calls pthread_kill(&thread, SIGUSR1) to wake up each waiting thread.

    If SIGUSR1 is sent before a call to sigwait, sigwait will return immediately. Thus, there will not be a race between adding a thread to the wait list and calling sigwait.

    EDIT:

    Why is CAS faster than a mutex? Laymen’s answer (I’m a layman). Its faster for some things in some situations, because it has lower overhead when there is NO race. So if you can reduce your concurrent problem down to needing to change 8-16-32-64-128 bits of contiguous memory, and a race is not going to happen very often, CAS wins. CAS is basically a slightly more fancy/expensive mov instruction right where you were going to do a regular “mov” anyway. Its a “lock exchng” or something like that.

    A mutex on the other hand is a whole bunch of extra stuff, that gets other cache lines dirty and uses more memory barriers, etc. Although CAS acts as a memory barrier on the x86, x64, etc. Then of course you have to unlock the mutex which is probably about the same amount of extra stuff.

    Here is how you add an item to a linked list using CAS:

    while (1)
    {
      pOldHead = pHead;  <-- snapshot of the world.  Start of the race.
      pItem->pNext = pHead;
      if (CAS(&pHead, pOldHead, pItem))  <-- end of the race if phead still is pOldHead
        break; // success
    }
    

    So how often do you think your code is going to have multiple threads at that CAS line at the exact same time? In reality….not very often. We did tests that just looped adding millions of items with multiple threads at the same time and it happens way less than 1% of the time. In a real program, it might never happen.

    Obviously if there is a race you have to go back and do that loop again, but in the case of a linked list, what does that cost you?

    The downside is that you can’t do very complex things to that linked list if you are going to use that method to add items to the head. Try implementing a double linked list. What a pain.

    EDIT:

    In the code above I use a macro CAS. If you are using linux, CAS = macro using __sync_bool_compare_and_swap. See gcc atomic builtins. If you are using windows, CAS = macro using something like InterlockedCompareExchange. Here is what an inline function in windows might look like:

    inline bool CAS(volatile WORD* p, const WORD nOld, const WORD nNew) { 
      return InterlockedCompareExchange16((short*)p, nNew, nOld) == nOld; 
    }
    inline bool CAS(volatile DWORD* p, const DWORD nOld, const DWORD nNew) {
      return InterlockedCompareExchange((long*)p, nNew, nOld) == nOld; 
    }
    inline bool CAS(volatile QWORD* p, const QWORD nOld, const QWORD nNew) {
      return InterlockedCompareExchange64((LONGLONG*)p, nNew, nOld) == nOld; 
    }
    inline bool CAS(void*volatile* p, const void* pOld, const void* pNew) {
      return InterlockedCompareExchangePointer(p, (PVOID)pNew, (PVOID)pOld) == pOld; 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an interview test question not homework. The test has been done. Which
This is an Amazon interview Question.I have solved this problem in O(n) using dynamic
I was asked in an interview this question LinkedList A has {1,2,3,4,5,6} LinkedList B
This is an interview question that I am using as a programming exercise. Input:
I had an interview question asking this: text file has following lines> 1: A
I hope that this question has not been asked elsewhere, but I'm having difficulty
This is an interview question . How would you determine if someone has won
This is an interview question I faced recently. Given an array of 1 and
This is an interview question asked a month ago.... Do session use cookies? If
This is an interview question. I have K machines each of which is connected

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.