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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:07:45+00:00 2026-05-27T07:07:45+00:00

I would like to be able to force a context switch from one thread

  • 0

I would like to be able to force a context switch from one thread to another. Therefore, I have implemented the following locking procedure:

#define TRUE  (1==1)
#define FALSE (0==1)

#include <pthread.h>

int acquire(void);
int release(void);
int c_yield(int count);

// Who was the last to acquire the lock
static volatile pthread_t lock_owner;

// Is the lock currently taken
static volatile int lock_taken = FALSE;

/* This variable indicates how many threads are currently waiting for
 * the lock. */
static volatile int lock_wanted = 0;

/* Mutex for protecting access to lock_wanted, lock_owner and
 * lock_taken */
static pthread_mutex_t mutex;

/* Condition even to notify when the lock becomes available */
static pthread_cond_t cond;

void init_lock(void) {
    pthread_cond_init(&cond, NULL);
    pthread_mutex_init(&mutex, NULL);
}

int acquire(void) {
    pthread_mutex_lock(&mutex);
    if(lock_taken) {
        lock_wanted++;
        pthread_cond_wait(&cond, &mutex);
        lock_wanted--;
    } 
    if(lock_taken) {
        pthread_mutex_unlock(&mutex);
        return EPROTO;
    } 
    lock_taken = TRUE;
    lock_owner = pthread_self();
    return pthread_mutex_unlock(&mutex);
}

int release(void) {
    pthread_mutex_lock(&mutex);
    lock_taken = FALSE;
    if(lock_wanted > 0) {
        pthread_cond_signal(&cond);
    }
    return pthread_mutex_unlock(&mutex);
}

Using another method (not shown), I can then implement a yield() that only returns if there are either no threads waiting for the lock, or after at least one other thread had a chance to run.

This implementation works fine most of the time, but if I stress-test it with ~50 threads trying to acquire and release the lock in random intervals, every once in a while acquire() will return EPROTO, indicating that someone called pthread_cond_signal without setting first lock_taken = FALSE.

Why is that? It seems as if the CPU sometimes doesn’t see the new value of lock_taken, which is why I already made the variables volatile. But it’s still happening…

  • 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-27T07:07:46+00:00Added an answer on May 27, 2026 at 7:07 am
    if(lock_taken) {
        lock_wanted++;
        pthread_cond_wait(&cond, &mutex);
        lock_wanted--;
    } 
    

    This should be while(lock_taken), not if. There are several reasons you might wake from pthread_cond_wait but find the lock taken by another thread by the time you get scheduled. One is if there’s a spurious wakeup. The other is if another thread enters acquire after we block, finds the lock not taken, and takes it itself before this thread gets the mutex again.

    The canonical way would be:

    lock_wanted++;
    while(lock_taken) pthread_cond_wait(&cond, &mutex);
    lock_wanted--;
    

    Get rid of all the volatiles, they harm performance and are not needed. Because mutexes are sufficient synchronization, nothing else is necessary. (And, to anyone else looking at your code, they signal that you don’t understand thread synchronization and tried to ‘sprinkle them in’ until it just happened to work.)

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

Sidebar

Related Questions

I have a string property that I would like to be able to force
I would like to be able to obtain all the parameter values from the
I use the following code and I would like to be able to get
I have a div inside a div and I would like to be able
I would like to be able to have a version number appended to a
Would like to be able to set colors of headings and such, different font
I would like to be able to loop through all of the defined parameters
We would like to be able to nightly make a copy/backup/snapshot of a production
I would like to be able to use the Tab key within a text
I would like to be able to display some dynamic text at the mouse

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.