Jump to content

  • Log In with Google      Sign In   
  • Create Account


multi-threading properly


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 ic0de   Members   -  Reputation: 804

Like
0Likes
Like

Posted 24 April 2013 - 08:59 PM

I'm trying to make an optimized all software version of an OpenCL based ray-caster that I wrote a while ago and I'm struggling to find an efficient way to manage my threads. 

 

What I want to do is have each thread casting rays into a different slice of the screen and then when they all finish show the rendered screen and repeat. I have a version that works but it is not ideal.

 

This is a slightly simplified version of my current code:

	while(1) //render loop
	{
		for(int i = 0; i < numCpus; i++)
		{
                        //re-create thread
			threads[i] = SDL_CreateThread(drawrange, "", (void*)((i+1)*screen->w/numCpus));
		}

		for(int i = 0; i < numCpus; i++)
		{
			SDL_WaitThread(threads[i], NULL); //wait for thread to finish executing
		}

		SDL_UpdateWindowSurface(mainwindow);
	}

 

This is not ideal because I re-create the thread every time in the loop, which to my knowledge is inefficient. Semaphores, mutexes and the like all seem to try to stop two threads from doing something at the same time, I need to make sure that they do things at the same time. Anyone have an idea for a more efficient solution?

 

Because I only need simple multi-threading and I already use SDL I used SDL's multi-threading api.


Edited by ic0de, 24 April 2013 - 09:03 PM.

you know you program too much when you start ending sentences with semicolons;


Sponsor:

#2 Hodgman   Moderators   -  Reputation: 28464

Like
4Likes
Like

Posted 24 April 2013 - 11:12 PM

Yeah you shouldn't be creating and destroying the threads every loop iteration; you should be able to create/destroy them just once and re-use them.
 
You can move your while(1) loop into the thread's main function, so that the thread will continue to loop.
To synchronize the threads, so that they all loop together, you can use a mutex to control some kind of "command" structure. Each thread has it's own command/task, with it's own mutex, which ensures either the worker is doing the task, or the main thread is generating the task (but never both at once). Because each thread has their own task/mutex, they can all be doing work at the same time.
Something like:
struct RenderTask
{
   Mutex lock;
   int threadId;
   int numThreads;
   Scene* input;
   Window* output;
   bool complete;
};
 
main thread:
  //make one job per thread
  RenderTask* tasks = new RenderTask[numThreads];
  fill in task with parameters
  create one thread per task, passing (void*)tasks[threadIndex] as the argument
  while(1)
  {
    int numComplete = 0;
    for each thread
    {
      lock threads task
        if task is complete
          ++numComplete
      unlock threads task
    }

    if( numComplete == numThreads )//all jobs are done
    {
      lock each threads task
        fill in all tasks with data for next frame (including setting complete to false)
        display results to screen
      unlock each threads task
    }
  }

worker thread:
  while(1)
  {
    lock task
      if task isnt complete
      {
        do task
        task.complete = true
      }
    unlock task
  }

Edited by Hodgman, 24 April 2013 - 11:15 PM.


#3 kauna   Crossbones+   -  Reputation: 2278

Like
0Likes
Like

Posted 25 April 2013 - 12:26 PM

Have you tried using the parallel pattern library? 

 

http://msdn.microsoft.com/en-us/library/dd492418.aspx

 

Cheers!



#4 ic0de   Members   -  Reputation: 804

Like
0Likes
Like

Posted 25 April 2013 - 05:48 PM

Have you tried using the parallel pattern library? 

 

http://msdn.microsoft.com/en-us/library/dd492418.aspx

 

Cheers!

 

I don't really think it's a problem with the library i'm using but rather my implementation.


you know you program too much when you start ending sentences with semicolons;


#5 kauna   Crossbones+   -  Reputation: 2278

Like
2Likes
Like

Posted 25 April 2013 - 05:59 PM

Well I didn't say that there was a problem with your current way of doing things. I just gave you some other way to handle threading. The PPL has some functions which make it pretty easy to implement multithreading especially in simple "embarrassingly parallel" cases. 

 

Cheers!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS