multi core programming (C++)

Programming, for all ages and all languages.
Post Reply
User avatar
Zacariaz
Member
Member
Posts: 1069
Joined: Tue May 22, 2007 2:36 pm
Contact:

multi core programming (C++)

Post by Zacariaz »

I have this function, which basicly is creating a list of primes from 0-2^32-1, however im not sure it works correctly, so im testing it right now. However, as you might imagine, this is gonna take some time and then i thought: "hey, i have two cores! In theory i could double the speed!" The calculations allows it, but finding resources on the matter has proved more difficult than expected, so if someone could just give me a little tiny push in the right direction, i would be forever gratefull.

Regards
This was supposed to be a cool signature...
User avatar
AndrewAPrice
Member
Member
Posts: 2299
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

You need to split the function into two and know which parts you want to run on either processor, which might be hard if you don't know how the function works.

On Windows you create a 2nd thread (http://www.codersource.net/win32_multithreading.html). Normally Windows will take care of distributing threads across processors. But you can manually chose which cores to use (Google "processor affinity").

On Unix, I'm not sure how multi-threading works, but you could try forking the process (simple with fork() ;)) and compare the old/new PID to see if you're in the new process or not, then set up a pipe between the two programs to share results. The Window's way seems easier (I'm sure Unix has threads but I don't know how they work, sorry).
My OS is Perception.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

Most modern *nixes support the pthreads (POSIX threads) library.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post by Solar »

Boost provides multithreading primitives, too. (Since you mentioned C++ in the thread title.)
Every good solution is obvious once you've found it.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

There is also the Threading Building Blocks library from Intel.
Post Reply