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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:04:16+00:00 2026-06-11T12:04:16+00:00

Since some languages don’t offer mutex lock, one will need to build it by

  • 0

Since some languages don’t offer mutex lock, one will need to build it by one’s self. I usually write some code like this: (pseudo-C++, just to let you know what I mean. I’m not sure whether my writing is correct as I haven’t written C++ code in years)

oldval = 0;
newval = 1;
//suggest there was a Compare And Set operation that make only one thread return true,if no CAS operation , call any other language module that can offer a cas instead
while (!CAS(*somepointer,oldval,newval)) {
     //lock failed
     sleep(1000); //sleep one second or some value really depend on what you are doing.
}
//lock succeed, do something....
*somepointer = oldval; //release lock.

Obviously sleep will lower the speed of processing, but I used to worry about, if sleep is discarded… wouldn’t threads switching too frequently and lower the performance? Now that I think deeper, how is sleep implemented? If they do something like this…

expired = getCurrentTime() + sleepTime;
while (getCurrentTime() < expired) { }

… this would only get things worse.

Now I think sleep should only be used if I really need to suspend the thread for a while, so maybe my case is just wrong.

  • 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-11T12:04:17+00:00Added an answer on June 11, 2026 at 12:04 pm

    sleep() and related functions make a system call to the OS which requests that the OS wake up the process in a set amount of time. The OS may wake the process sooner in certain cases (e.g. receipt of a signal/exception), but the process will otherwise get zero CPU usage during the wait.

    If the OS finds that all processes are sleeping (or blocked on some kind of timeout), it can go to sleep itself by using a hardware-specific instruction to sleep the processor until an interrupt (e.g. clock interrupt, DMA interrupt, etc.) is received.

    OS-provided synchronization primitives work similarly. For example, asking to lock a mutex results in a system call that puts the thread to sleep until the mutex is unlocked; the OS knows that the mutex is unlocked since the unlock must also go through a system call.

    Using sleep uses a lot less CPU compared to a simple busy-wait loop. However, your OS probably provides at least some synchronization primitives (even embedded systems frequently provide these if they support threads in any way), such as semaphores or locks. You should try to find out if your OS provides these functions before trying to roll your own; your application will have better performance and responsiveness if you use OS-provided synchronization primitives.

    Important note: If you are developing on a bare-metal embedded platform, the library sleep may in fact be a busy wait (like it is on e.g. Arduino), since there’s no OS scheduler to put threads to sleep. In this case, if minimizing processor usage is important (e.g. to reduce power consumption), you will need to find a processor-specific method of suspending the CPU if your library doesn’t provide one.

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

Sidebar

Related Questions

I'm using Python since some times and I am discovering the pythonic way to
i am driving since some years a sql-server2000 merge-replication over three locations. Triggers do
I have an R package on github that uses a configure script (since some
I am doing an archive service for logs. Since some files are open and
i`m working on my assignment for univ, and since some parts are not really
Magento Orders->Google Sitemap ... since recently a blank page?!!!! Hi since some weeks our
I find Oleg's docs on Iteratee somewhat difficult to get into. Especially since some
I've been doing some cocoa since a week, and I'm getting kinda good with
I've been using Oracle for quite some time since Oracle 8i was released. I
This might be a simple question for some people since it was quite hard

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.