Tuesday, 12 September 2006

Multithreading in C++

The Powerglove periodically squeezes out its datas. One way to keep the informations up-to-date is to delegate the readings of its status to a separate thread, which stores data in a circular buffer. This can be achieved with the multithreading tecnique: one process but more than one separate, and virtually parallel, thread of execution. It's gonna be my "producer".

The "consumer" will be the main procedure, which will obtain the deglitched coordinates with specifically concurrent methods. I'm wondering if a separate thread for the MIDI could be useful.

Btw, for anyone who'd like to write multithreaded classes, here's my snippet:


void threadFunc() {
  // actual thread routines
}

void launcher()
{
  // you can pass useful parameters to the thread using this variable
  long * param;

  // the "threadID" variable will contain the reference of the created thread
  DWORD threadID;

  // the NULL and the zeros are additional features of the syscall
  // check out references for further info
  // they're not needed for a simple thread creation
  HANDLE ret = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) threadFunc, param, 0, &threadID);
}

1 comment:

AkiRoss' said...

Mh, why does this code smell like windows?

Nono you're using windows threading API with a portable library like OpenAL :D The best way to f.k portableness ;)

Boost.thread is the way ;)
http://www.boost.org/doc/html/threads.html

Hey dude, don't you have jabber? :D I had to add you in msn, but... it's painful to me!