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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:22:58+00:00 2026-05-17T00:22:58+00:00

I have one main thread that does some rather CPU intensive operation. The thread

  • 0

I have one main thread that does some rather CPU intensive operation. The thread has to hold a lock for all its calculations.

Then there are some other threads which occasionally require the same lock for brief amounts of time.

How can I force the main the main thread to occasionally allow the other threads to execute without slowing it down if there are no other threads?

A periodic

lock.release()
time.sleep(x)
lock.acquire()

for some x would pass control to another thread, but slow the main thread down if there are no other threads.

On the other hand, without the sleep() call the GIL seems to interfere here. Since the main thread has the GIL when it executes release(), the other thread is apparently not able to return from acquire() immediately. Therefore, the main thread continues with its own acquire() method and the other threads never get the lock unless a GIL switch happens to coincide exactly with me releasing my own lock.

What’s the proper solution for this problem? Is there a way to force a GIL release? Basically I want some magic piece of code that makes sure that in the following test script the second thread always gets the lock first:

import threading
import time

class t1 (threading.Thread):
    def run(self):
    print "Second thread waiting for lock"
    lock.acquire()
    print "Second thread got lock"
    lock.release()

lock = threading.Lock()
lock.acquire()

t = t1()
t.start()
time.sleep(1)

lock.release()
time.sleep(0) # this works very often, but not all the time
lock.acquire()
print "Main thread got lock"
lock.release()
  • 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-17T00:22:59+00:00Added an answer on May 17, 2026 at 12:22 am

    Merely releasing the GIL doesn’t guarantee that other threads will have a chance to run.

    In Unix, the call you really want is sched_yield(). There’s no interface to that function in the Python standard library; it would be straightforward to add one with a native module.

    usleep(0) and select() are sometimes used for the same purpose, though it’s not always the same depending on the system scheduler. Windows, you want Sleep(0). Use time.sleep(0) for both of those.

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

Sidebar

Related Questions

I have an application that has two threads. The first one (the main thread)
In one of Outlook add in, I have a worker thread that does some
I have a thread that does several things. One of them is to just
I have one main class with several inherited members who all overload the same
I have one main parent swf that loads several other swfs. If something happens
If I have two threads (Linux, NPTL), and I have one thread that is
I have app that uses two threads: one thread for regular application work and
Does one of you has an idea of how to catch in the main
I have one main windows form and within that form I have custom controls
I have one thread that inserts into the queueStream (not shown here) and FlowController

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.