Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Question regarding multithreading

This thread is locked; no one can reply to it. rss feed Print
Question regarding multithreading
ryancoboy
Member #14,468
July 2012

I have never worked with multi-threading before (started learning about it a few days ago). it looked interesting and like a powerful tool, so i started messing around with it. Making a few test programs here and there.

In my latest test I have hit a roadblock that I can't seem to get myself around.

I have located the problem, but i am unaware as to why it is giving me a run-time-error. here is the code for the thread that is running and causing the error (note: this function works if not using it in a thread):

#SelectExpand
1void *Load(ALLEGRO_THREAD *thr, void *arg) 2{ 3 std::cout << "Load thread: Started." << std::endl; 4 5 ThreadInfo *Info = (ThreadInfo*) arg; 6 7 al_get_bitmap_format(al_get_backbuffer(al_get_current_display())); 8 //LoadMap(); 9 10 al_lock_mutex(Info->mutex); 11 Info->State->CurState = Playing; 12 al_unlock_mutex(Info->mutex); 13 14 std::cout << "Load thread: Ended." << std::endl; 15 16 return NULL; 17}

The line that it gives the error on is:

I'm guessing it is because the call to al_get_bitmap(...) is not thread safe. But I'm still new to this multi-threading stuff. So if someone could confirm or deny, that be great. And any ideas on how to make this work using threads would also be appreciated.

Thanks in advanced,
-Ryan

-Ryan
----------------------------------------------------
If I'm asking, it's because I failed for over 2 hours trying to find the answer on Google / trying to fix it myself.

Todd Cope
Member #998
November 2000
avatar

The first thing you should do is check the return value of each function before passing it to the next.

ALLEGRO_DISPLAY * display;
ALLEGRO_BITMAP * bitmap;
int format;

display = al_get_current_display();
if(display)
{
    bitmap = al_get_backbuffer(display);
    if(bitmap)
    {
        format = al_get_bitmap_format(bitmap);
    }
}

Most likely, one of these functions is returning NULL and causing a crash when that value is passed to a function expecting a valid pointer.

Also, in your code you aren't storing the format anywhere. What exactly are you trying to do there?

ryancoboy
Member #14,468
July 2012

@Todd Cope: Thanks for the tips I'll try that here in a couple moments and post back here (dinner time ;D). As you can see, I have a function commented out "LoadMap()" and deep in that function is the al_get_bitmap_format(...) code. i just moved it up front so that i wouldn't have to post a billion function calls, etc. Once a solution is found to prevent the error I will attempt to fix it where it is actually intended to be used.

EDIT: I just tested it real quick. And "format" ended up being equal to 23. So it looks to be running fine there as well.

-Ryan
----------------------------------------------------
If I'm asking, it's because I failed for over 2 hours trying to find the answer on Google / trying to fix it myself.

Trent Gamblin
Member #261
April 2000
avatar

The manual states that each thread has it's own "current display", and new threads have a default NULL current display, so you're passing NULL into those functions and that's probably why it's crashing.

If you had done what Todd said, you would have figured that out.

ryancoboy
Member #14,468
July 2012

@Trent Gamblin: your right. I must of missed that part in the manual or forgot about it. Thanks for clearing that up for me. And i was in a rush for my dinner so i didn't test the return value of my display in my "Load" thread. I just did it in my main. I was rushing and didn't read the post completely. Sorry, for that.

That pretty much solves my problem.

Thanks guys.
-Ryan

-Ryan
----------------------------------------------------
If I'm asking, it's because I failed for over 2 hours trying to find the answer on Google / trying to fix it myself.

Go to: