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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:44:29+00:00 2026-06-05T09:44:29+00:00

I am trying to compare the performance of boost::atomic and pthread mutex on Linux:

  • 0

I am trying to compare the performance of boost::atomic and pthread mutex on Linux:

 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER ;
 int g = 0 ;

 void f()
 {

    pthread_mutex_lock(&mutex);
    ++g;
    pthread_mutex_unlock(&mutex);
    return ;
 }
 const int threadnum = 100;
 int main()  
 {
    boost::threadpool::fifo_pool tp(threadnum);
    for (int j = 0 ; j < 100 ; ++j)
    {
            for (int i = 0 ; i < threadnum ; ++i)
                    tp.schedule(boost::bind(f));
            tp.wait();
    }
    std::cout << g << std::endl ;
    return 0 ; 
 }

its time:

 real    0m0.308s
 user    0m0.176s
 sys     0m0.324s

I also tried boost::atomic:

 boost::atomic<int> g(0) ;

 void f()
 {

      ++g;
    return ;
  }
  const int threadnum = 100;
  int main()
  {
    boost::threadpool::fifo_pool tp(threadnum);
    for (int j = 0 ; j < 100 ; ++j)
    {
            for (int i = 0 ; i < threadnum ; ++i)
                    tp.schedule(boost::bind(f));
            tp.wait() ;
    }
    std::cout << g << std::endl ;
    return 0 ;
   }

its time:

 real    0m0.344s
 user    0m0.250s
 sys     0m0.344s

I run them many times but the timing results are similar.

Can atomic really help avoid overhead of sys calls caused by mutex/semaphore ?

Any help will be appreciated.

Thanks

UPDATE : increase the loop number to 1000000 for

    for (int i = 0 ; i < 1000000 ; ++i)
    {
            pthread_mutex_lock(&mutex);
            ++g;
            pthread_mutex_unlock(&mutex);
    }

similar to boost::atomic .

test the time by “time ./app”

use boost:atomic:

real    0m13.577s
user    1m47.606s
sys     0m0.041s

use pthread mutex:

real    0m17.478s
user    0m8.623s
sys     2m10.632s

it seems that boost:atomic is faster because pthread use more time for sys calls.

Why user time + sys is larger than real time ?

Any comments are welcome !

  • 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-05T09:44:31+00:00Added an answer on June 5, 2026 at 9:44 am

    I guess you’re not correctly measuring the time taken by atomics vs mutexes. Instead, you’re measuring the overhead incurred by the boost thread pool management: it takes more time to setup a new task f() than executing the task itself.

    I suggest you add another loop in f() to obtain something like this (do the same for the atomic version)

     void f()
     {
        for(int i = 0  ; i < 10000 ; i++) {
          pthread_mutex_lock(&mutex);
          ++g;
          pthread_mutex_unlock(&mutex);
        }
        return ;
     }
    

    Please post the score if something changed, I’d interested to see the difference !

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

Sidebar

Related Questions

I am trying to compare performance between raw pointers, boost shared_ptr and boost weak_ptr.
I am trying to compare the performance of boost::multi_array to native dynamically allocated arrays,
I am trying to compare the performance of several Javascript libraries. Although measuring transaction
I am trying to compare the performance of a specific F# benchmark running on
I'm trying to compare GPU to CPU performance. For the NVIDIA GPU I've been
I am trying to see if the performance of both can be compared based
Trying to compare a given Time between two times to see if it's within
Trying to compare and print only members with Inactive status. The problem is that
When trying to compare two objects passed as arguments: first as object, second as
Im trying to compare these two chars but on win 32 Visual Studio 2008:

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.