views:

1386

answers:

8

I'm a kinda newbie developer with a few years under my belt. Recently I interviewed at a game company and was asked "have you done any multi-threading?" I told them about having a C# app with a few Threads... and then I said a bit about transactions and locking etc in Sql. The interviewer politely told me that this was too high-level and they are looking for someone with experience doing multi-threading in C++.

So what is a basic example of "low-level" multi-threading in C++ ?

+12  A: 

The canonical implementation of "low level threads" is pthreads. The most basic examples of threading problems that are usually taught along with pthreads are some form of readers and writers problem. That page also links to more classical threading problems like producers/consumers and dining philosophers.

Ryan
+2  A: 

I'd take a look at http://zthread.sourceforge.net/ which is an excellent wrapper around PThreads library. It's a very fast and stable and can be rather low-level library. Very well written and documented.

Reflog
+2  A: 

I don't think there's much difference between what you've been doing and using pthreads, to be honest. If you've done a significant amount of work with threads you'll have had all the issues with locking, synchronisation etc, and could pick up using pthreads calls directly easily enough. The one thing that's slightly tricky that you've probably been insulated from is termination, getting exit codes out of the threads etc.

(Of course, pthreads isn't as low level as you can get; if you're on linux, look at clone() to see how it really works on a system call level. But no-one actually uses that unless they're implementing pthreads.)

Mark Baker
+3  A: 

Here's a quick example of using pthreads, based on some test code of ours. It spawns two threads, and waits for them to complete.

int main( void )
{
  pthread_t reader, writer;
  void *arg;

  // [... initialisation ...]

  // Spawn threads
  if( pthread_create( &reader, NULL, reader_func, arg ) ||
      pthread_create( &writer, NULL, writer_func, arg ) )
    {
      perror( "pthread_create" );
      return EX_OSERR;
    }

  // Wait while threads run
  pthread_join( reader, &arg );
  pthread_join( reader, &arg );

  return EX_OK;
}

void *reader_func( void *arg )
{
  // [... do some stuff ...]
  return NULL;
}

void *writer_func( void *arg )
{
  // [... do some different stuff ...]
  return NULL;
}
Mark Baker
+1  A: 

well if you really want to take this idea to the "max" and you are willing to get your hands dirty with threads. I would recommend trying to implement a user space thread library.

look into uconext.h and its various procedures (setcontext, swapcontext and makecontext) and try to write a simple cooperative thread library with them. implement locks, condition variables thread creation/destruction and cooperative yielding. It won't be particularly fancy (i.e. there is no actualy parallelism (for that you need to ether dig into the clone system call or the kernel) but if you can do it no one will be able to say you don't have low level experience.

Just to give a sense of the scale of the project. I wrote just such a simple thread library in about 500 lines of c++, and considering that at least 20% of that was comments, assert statements and logging for debugging purposes.

luke
+3  A: 

He was probably referring to your use of C#, not your threading experience.

jproffer
+1  A: 

Also Boost Threads is fairly portable wrapper around pthreads and Windows threads... and from what I hear many game dev shops use C++ and like some of the Boost libraries.

ceretullis
+2  A: 

If it is a game company, then they probably want an answer something like this:

"I implemented a multithreaded AI routine that allocated decision-tree calculations for NPCs among the available CPU resources. Correct locking, especially when integrating the code with the rest of the app, was difficult. We also spent some time tracking per-thread resource usage so we could throttle back the AI processing when it threatened to interfere with main UI responsiveness."

(I made all that up, I personally have not actually done any of that. grin)

Bruce