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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:05:08+00:00 2026-05-29T10:05:08+00:00

So recently I started thinking about a way of improving speed (vs cpu usage)

  • 0

So recently I started thinking about a way of improving speed (vs cpu usage) in a program which is extensible by DLL’s which you can built with the provided SDK (Software Development Kit) in C++.

I started doing research on threading data and then writing to global variables, where – if needed – the main thread would just call a variable, and not execute a whole function (the thread does the stuff).

I was actually surprised by my results, because I expected the thread to crash the application – however it didn’t.

I started a discussion on the application’s developer forum – we got to the point that stated:
“The rule for threads is: If it crashes, it’s wrong, if it doesn’t crash, it’s probably still wrong.” and:
“Actually, that code probably won’t crash, but it can corrupt variables. I’ve heard stories of multi-threaded programs running for months with no issues before a race-hazard occurred.”

Well, can ‘GETS’ actually cause race conditions?

The main application (SA-MP , San Andreas Multiplayer) is single threaded, and compiled like that.

The code I used for my tests:
[C++]

#ifdef OS_WINDOWS
    void Thread::BackgroundCalculator( void *unused )
#else
    void *Thread::BackgroundCalculator( void *unused )
#endif
{
    float X;
    float Y;
    float Z;
    while( true )
    {
        if(ENABLED == false)
        {
            continue;
        }
        for(int i = 0; i < MAX_PLAYERS; ++i)
        {
            if(IsPlayerConnected(i) == false)
            {
                speed[i] = -1.0f;
                continue;
            }
            if(IsPlayerInAnyVehicle(i) == true)
            {
                GetVehicleVelocity(GetPlayerVehicleID(i),&X,&Y,&Z);//Is actually used by MAIN APPLICATION thread too... so should cause race condition?
            }
            else
            {
                GetPlayerVelocity(i,&X,&Y,&Z);//Is actually used by MAIN APPLICATION thread too... so should cause race condition?
            }
            speed[i] = sqrt(X*X+Y*Y+Z*Z);//called from my code.. thread
        }
        SLEEP(30);
    }

    EXIT_THREAD();//should be never reached..
}

static cell AMX_NATIVE_CALL n_GetSpeed( AMX* amx, cell* params )
{
    return amx_ftoc(speed[params[1]]);//returning to main thread, ftoc = FloatToCell
}

and when calling VERY MANY TIMES “n_GetSpeed” from the virtual machine (.amx file). It doesn’t crash.
I also tried calling GetPlayerVelocity/GetVehicleVelocity from the main thread and my thread as much as possible at the same time. and still doesn’t crash. totally not what I’d expect.

So, when we got to the point where somebody said a race-condition will occur, I would like to know how to cause one (force a race condition?). (in C/C++ ofcourse)

__

The code I am making is always open-source and available from my page, this too:
http://gpb.googlecode.com/files/ThreadTest_R100.zip
Just if you need the whole code 😉

__

Extra note: the thing is.. That I only access variables, not change, the only thing I change is the speed variable and it’s ONLY done in the second thread.

  • 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-29T10:05:09+00:00Added an answer on May 29, 2026 at 10:05 am

    Without knowing much about the application you are modifying. It could potentially cause a crash.

    You say that you only read, so everything should be fine. BUT, the main thread (the original program) will most definitely change the variables. One thing that could happen is that after you call IsPlayerInAnyVehicle and it returns TRUE, it changes to FALSE, or the vehicle changes, or the player dies, or the player quits, … Your call to GetVehicleVelocity, GetPlayerVehicleID or GetPlayerVelocity might therefore be called under erroneous circumstances. Will this lead to a crash? Who knows. But at least, the code is not 100% healthy.

    EDIT: It is impossible to know whether this code is thread-safe without knowing how the methods you call are implemented. Put weak memory ordering into the game and all bets are off, without proper memory barriers.

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

Sidebar

Related Questions

I recently started using Emacs for editing. When I edit english-language files (like program
I was thinking recently about what projects I could start that would be of
I recently saw an OO design question on some forum and started thinking of
I recently started this question in another thread (to which Reed Copsey graciously responded)
I recently started learning about HttpModules and made my first one. I was wondering
Recently, I've been thinking about how there are multiple versions of linux and how
I recently started using Mercurial and like the way I have VCS protection for
I recently started work on a personal coding project using C++ and KDevelop. Although
I recently started a new webforms project and decided to separate the business classes
I recently started using Eclipse at work for my Java servlet projects. I've been

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.