jeudi 23 juillet 2015

std::string and multithread portability will this method work predictably on all of my target platforms

I have a application which makes use of the following.

  1. C++11 STD library.

  2. SDL 2.x

  3. SDL_Image 2.0

  4. 2 active threads.

Taget platfroms:

Win x32 mingw

Win x64 mingw

OSX x64 clang

Linux x32 gcc

Linux x64 gcc

What I need to know is if this will work safely and consistantly on all my targets.

Code portions:

Thread 0 code:

SDL_Surface* LoadSurface(string file)

{

//Initialize to nullptr 

    SDL_Surface *image = nullptr;

    //Load the image

    image = IMG_Load(file.c_str());

    if (image == nullptr)

    {

      std::cout << "SDL_CreateSurface Error: "  << SDL_GetError() << std::endl;

     //failsafe to prevent a null return -and- make error obvious when executed. 
     image = SDL_CreateRGBSurface(0, 800, 600, 32, 0, 255, 0, 255);

    }

    return image;

    SDL_FreeSurface(image);

}

void DrawScreen()

{

...snip...

SDL_Surface *worksurface = LoadSurface(display.layer[1].tile[1].srctexture);

...snip...

}

Thread 1 code:

int BuildScreenData()
{

...snip...
display.layer[1].tile[1].srctexture = "texture.bmp";
...snip...

return(0);
}

display structure:

 struct TILE
 {
 string srctexture;
 atomic<int> tilesize;
 atomic<int>  srcx;
 atomic<int>  srcy;
 atomic<int>  destx;
 atomic<int>  desty;
 };

struct MLAYER
{
 TILE tile[1024];
};

struct DISPLAYSCREEN
{
 atomic<int>  solidfill;
 atomic<int>  fillr;
 atomic<int>  fillg;
 atomic<int>  fillb;
 MLAYER layer[7];
};

DISPLAYSCREEN display;

So far I have noticed no significant issues with OSX Yosemite or 32bit windows 8.1.

But I still have the idea that I'm missing a safer way to do this.

Aucun commentaire:

Enregistrer un commentaire