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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:30:56+00:00 2026-05-11T14:30:56+00:00

I’m developing a Ray Tracer in C++ using SDL and Pthread. I’m having issues

  • 0

I’m developing a Ray Tracer in C++ using SDL and Pthread. I’m having issues making my program utilize two cores. The threads work, but they don’t use both cores to 100%. To interface SDL I write directly to it’s memory, SDL_Surface.pixels, so I assume that it can’t be SDL locking me.

My thread function looks like this:

void* renderLines(void* pArg){ while(true){     //Synchronize     pthread_mutex_lock(&frame_mutex);     pthread_cond_wait(&frame_cond, &frame_mutex);     pthread_mutex_unlock(&frame_mutex);      renderLinesArgs* arg = (renderLinesArgs*)pArg;     for(int y = arg->y1; y < arg->y2; y++){         for(int x = 0; x < arg->width; x++){             Color C = arg->scene->renderPixel(x, y);             putPixel(arg->screen, x, y, C);         }     }      sem_post(&frame_rendered);     } } 

Note: scene->renderPixel is const, so I assume both threads can read from the same memory. I have two worker threads doing this, in my main loop I make these work using:

//Signal a new frame pthread_mutex_lock(&frame_mutex); pthread_cond_broadcast(&frame_cond); pthread_mutex_unlock(&frame_mutex);  //Wait for workers to be done sem_wait(&frame_rendered); sem_wait(&frame_rendered);  //Unlock SDL surface and flip it... 

Note: I’ve also tried creating and joining the threads instead of synchronizing them. I compile this with ‘-lpthread -D_POSIX_PTHREAD_SEMANTICS -pthread’ and gcc does not complain.

My problem is best illustrated using a graph of the CPU usage during execution: CPU usage during execution.
(source: jopsen.dk)

As can be seen from the graph my program only uses one core at a time, then switching between the two every once in a while, but it doesn’t drive both to 100% ever. What in the world have I done wrong? I’m not using any mutex or semaphors in scene. What can I do to find the bug?

Also if I put while(true) around scene->renderPixel() I can push both cores to 100%. So I’ve suspected that this is caused by overhead, but I only synchronize every 0.5 second (e.g. FPS: 0.5), given a complex scene. I realize it might not be easy to tell me what my bug is, but an approach to debugging this would be great too… I haven’t played with pthreads before…

Also, can this be a hardware or kernel issue, my kernel is:

$uname -a Linux jopsen-laptop 2.6.27-14-generic #1 SMP Fri Mar 13 18:00:20 UTC 2009 i686 GNU/Linux 

Note:

  • 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. 2026-05-11T14:30:57+00:00Added an answer on May 11, 2026 at 2:30 pm

    This is useless :

    pthread_mutex_lock(&frame_mutex); pthread_cond_wait(&frame_cond, &frame_mutex); pthread_mutex_unlock(&frame_mutex); 

    if you wait to wait for a new frame do something like :

    int new_frame = 0;

    First thread :

    pthread_mutex_lock(&mutex);  new_frame = 1;  pthread_cond_signal(&cond); pthread_mutex_unlock(&mutex); 

    other thread :

    pthread_mutex_lock(&mutex);  while(new_frame == 0)   pthread_cond_wait(&cond, &mutex);  /* Here new_frame != 0, do things with the frame*/ pthread_mutex_unlock(&mutex);  

    pthread_cond_wait(), actually release the mutex, and unschedule the thread until the condition is signaled. When the condition is signaled the thread is waken up and the mutex is re-taken. All this happen inside the pthread_cond_wait() function

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

Sidebar

Ask A Question

Stats

  • Questions 149k
  • Answers 149k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The projection is something I've played with before. It doesn't… May 12, 2026 at 9:33 am
  • Editorial Team
    Editorial Team added an answer One key feature of TCP is "discarding duplicate packets" So… May 12, 2026 at 9:33 am
  • Editorial Team
    Editorial Team added an answer If I understand what you are doing, Python is not… May 12, 2026 at 9:33 am

Related Questions

I'm developing a site in Visual Web Dev Express, and when I run/debug, I'd
I'm developing a data access component that will be used in a website that
I'm developing a C# application that uses a handful of XML files and some
I'm developing a 3-column website using a layout like this: <div id='left' style='left: 0;

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.