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

  • Home
  • SEARCH
  • 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 9222063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:43:34+00:00 2026-06-18T03:43:34+00:00

new to programming in threads and using locks So I’ve 2 threads – one

  • 0

new to programming in threads and using locks
So I’ve 2 threads – one reading vlaues from COM port – the other one controlling a stepper motor

in one thread

        if stepperb_value != 0: #if stepperb non-zero
            if stepperb_value > 0: # if positive value
                self.step_fine(10,11,12,13,step_delay) #step forward
            else:
                self.step_fine(13,12,11,10,step_delay) #step backwards
            if abs(stepperb_value) != 100:
                time.sleep(10*step_delay*((100/abs(stepperb_value))-1))                       

(I need to prevent a change to stepperb causing a divide by zero error on last line)

in the other thread thats reading the values from the COM port

            if 'stepperb' in dataraw:
                outputall_pos = dataraw.find('stepperb')
                sensor_value = dataraw[(1+outputall_pos+len('stepperb')):].split()
                print "stepperb" , sensor_value[0]
                if isNumeric(sensor_value[0]):
                    stepperb_value =  int(max(-100,min(100,int(sensor_value[0]))))

Where (and what sort of locks) do I need – the first thread is time sensitive so that needs to have priority

regards

Simon

  • 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-18T03:43:35+00:00Added an answer on June 18, 2026 at 3:43 am

    If you’re using only CPython, you have another possibility (for now).

    As CPython has the Global Interpreter Lock, assignments are atomic. So you can pull stepperb_value into a local variable in your motor control thread before using it and use the local variable instead of the global one.

    Note that you’d be using an implementation detail here. Your code possibly won’t run safely on other python implementations such as Jython or IronPython. Even on CPython, the behaviour might change in the future, but I don’t see the GIL go away before Python 5 or something.

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

Sidebar

Related Questions

I am fairly new to Python programming and Threads isn't my area of expertise.
New to programming. I wrote a Python code (with help from stackoverflow) to read
I'm kinda new to concurrent programming, and trying to understand the benefits of using
I am new to Objective C programming. I have created two threads called add
I have searched through threads on Stackoverflow and other sites using the Google machine
I am quite new in programming and aspiring to make a game using cocos2dx
Iam new to c programming and needs some help. long *taskids[NUM_THREADS]; for(t=0; t<NUM_THREADS; t++)
I am pretty new to thread-safe programming, and was wondering if I have something
What features could be added to a new programming language to make it more
let's say that you in your new programming work your boss comes and says

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.