Sdl and threads

hi. I want to make a program that shows some squares in the screen and
the squares every time change its color. I made using pthreads and used
sdl_fillrect but appears some errors in the console (Xlib: unexpected
async reply (sequence 0x155a)! )
this problem happens because I used sdl commands in threads? I can use
mutex before calling the commands? if I cant use sdl commands in
threads, any ideas about how I can make that program?

thanks in advance

here is the threads function:

void *doAction(void *e){
entity *entity_ptr;
entity_ptr = (entity *) e;
for(;;){
int color[3];
color[1] = rand()%255;
color[2] = rand()%255;
color[3] = rand()%255;
entity_ptr->setRgbColor(color);
entity_ptr->draw();
}
}

I call sdl_fillrect in entity_ptr->draw();

void entity::draw(){
if (SDL_MUSTLOCK(SDL_GetVideoSurface()))
SDL_LockSurface(SDL_GetVideoSurface());
SDL_Rect rect;
rect.x = x;
rect.y = y;
rect.w = 9;
rect.h = 9;
randColor = SDL_MapRGB(SDL_GetVideoSurface()->format, rgbColor[0],
rgbColor[1], rgbColor[2]);
SDL_FillRect(SDL_GetVideoSurface(), &rect, randColor );
if (SDL_MUSTLOCK(SDL_GetVideoSurface()))
SDL_UnlockSurface(SDL_GetVideoSurface());
SDL_Flip(SDL_GetVideoSurface());
}