• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Multi-core programming techniques?

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

MarkS

Member
Joined
Feb 21, 2006
Location
Oklahoma City
Can someone point me towards some multi-core programming resources? I'm about to buy my first dual-core processor and I would love to start writing programs to use both. I originally thought that this meant writing a multi-threaded app, but it would seem that this is not the case.

Anyone here have experience with this?
 
Thanks. That looks good.

Does anyone have some simple (if such a thing can possibly exist) code examples?
 
It does mean writing multithreaded programs or have more than one process (like Unix daemons often do)
 
What language, what library, what application?
if C++, here is what a simple google thing gave me:

#include <cstdio>
#include <process.h>
#include <iostream>
void thread1(void* pVoid)
{
for(int i = 0; i < 100; i++){}
printf("thread1 Done\n");
}

void thread2(void* pVoid)
{
for(int i = 0; i < 100; i++){}
printf("thread2 Done\n");
}

int main()
{
_beginthread(thread1, 0, 0);
_beginthread(thread2, 0, 0);

system("PAUSE");
return 0;
}
 
Last edited:
This is a pretty good introduction to writing multithreaded Java apps.

Sample code (haven't tried compiling so I can't guarantee I didn't make any stupid errors :D)
Code:
public class CountThread implements Runnable {
   int min;
   int max;

   CountThread(int min, int max) {
      this.min = min;
      this.max = max;
   }

   //run() is required by the Runnable interface & is run when you start the thread
   public void run() {
      for (int i=min; i<=max; i++) {
         System.out.println(i);
      }
   }

   public static void main(String args[]) {
      CountThread thread1 = new CountThread(0,100);     //You may want to tweak the ranges
      CountThread thread2 = new CountThread(1000,1100); //For these two instances
      
      thread1.start(); //Start up the first thread
      thread2.start(); //Start up the second thread.
   }
}

JigPu
 
MarkS said:
Thanks. That looks good.

Does anyone have some simple (if such a thing can possibly exist) code examples?

This does an endless loop on two processors:

Code:
/* Error checking and proper argument declarations left as exercise for the user */
main() {
fork();
for (;;);
}
 
Back