简体   繁体   中英

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

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++ ?

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.

他可能指的是您对 C# 的使用,而不是您的线程经验。

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 )

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( writer, &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;
}

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.

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.)

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 (ie 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.

此外, Boost Threads是围绕 pthreads 和 Windows 线程的相当可移植的包装器......据我所知,许多游戏开发商店使用 C++ 并且喜欢一些 Boost 库。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM