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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:40:36+00:00 2026-05-27T17:40:36+00:00

i have a multithreaded library in which i call sleep(3) in different threads. i

  • 0

i have a multithreaded library in which i call sleep(3) in different threads.
i have written python bindings for it using boost python.
now it looks like boost python is messing up with the sleep(3) function as it pauses the whole python program to wait.

please consider i have this boostmod.cpp file

#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE( boostmod ) {
    def("waiter",&::sleep);
}

( you can compile it using: )

$ g++ -fPIC -shared boostmod.cpp `python-config --cflags --libs` -lboost_python -o boostmod.so

this is a python test file threadtest.py :

import time,sys,threading,boostmod
from ctypes import *
if __name__ == '__main__':
    libc = CDLL("libc.so.6") # this only works in linux
    for n in range(5):
         if sys.argv[1] == "boost":
            # this is slow
            threading.Thread(target=boostmod.waiter,args=(3,)).start()
         elif sys.argv[1] == "native":
            # this is fast
            threading.Thread(target=time.sleep,args=(3,)).start()
         elif sys.argv[1] == "ctypes":
            # this is fast
            threading.Thread(target=libc.sleep,args=(3,)).start()

the results are as follows:

$ time python threadtest.py boost
  real    0m15.030s
  user    0m0.024s
  sys     0m0.005s
$ time python threadtest.py native
  real    0m3.032s
  user    0m0.027s
  sys     0m0.003s
$ time python threadtest.py ctypes
  real    0m3.030s
  user    0m0.022s
  sys     0m0.008s

if you observe the situation with:

$ watch -n1 ps -C python -L -o pid,tid,pcpu,state

you can see that “native” and “ctypes” are really building up 5 threads plus the main thread while the “boost” case is only having one thread. Actually in the “boost” case the “.start()” is blocking inside the “sleep()” 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-05-27T17:40:36+00:00Added an answer on May 27, 2026 at 5:40 pm

    first of all, i have seen that the time.sleep function in python is not using the sleep(3) systemcall. it uses a timing out call of select(2) on stdin ( in function “floatsleep” ) so that it can be interrupted.

    However i have also found out that if you write a wrapper with Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS around the boost module function the phenomenon seems to dissolve.

    So Here is the new boost module code that allows multithreading with sleep(3) calls:

    #include <boost/python.hpp>
    using namespace boost::python;
    void waiter(int seconds) {
        Py_BEGIN_ALLOW_THREADS
        ::sleep(seconds);
        Py_END_ALLOW_THREADS
    }
    BOOST_PYTHON_MODULE( boostmod ) {
        def("waiter",&::waiter);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multithreaded Java code in which: several threads read stateful objects from
im just installing the boost library using an installer. Its asking me which variants
I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library.
I have a long-running multithreaded program, and I'd like to occasionally like to call
I have a speed critical multithreaded program which involves data in a tree structure.
I have a graphical control that is multithreaded. Until now it worked fine, but
I have started to play around with boost python a bit and ran into
I have a multithreaded Windows application where one of the threads has a message
I'm developing application using VC++ 6. I have a 3rd party DLL. This library
i have written a library with some classes that make use of qt object

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.