Having problemswith multithreading and prime numbers

Posted on 16th Feb 2014 07:03 pm by admin

I have an assignment when I'm suppose to do the following:

Write a multithreaded Java, Pthreads, or Win32 program that outputs prime numbers. This program should work as follows:
The user will run the program and will enter a number on the command line.
The program will then create a separate thread that outputs all the prime numbers less than or equal to the number entered by the user.

I have the multithreading code and the prime number although i don't know where to place it. does anyone think they can help me. Thanks in advance.

// First, always include for all the Win32 specific thread information
#define MAX_THREADS 3
int isprime ;
// Prototypes are good and handy, but not necessary in this example.
// These three functions are run by each of our three threads
// Please note how the functions are declared:
// In Win32, thread functions MUST be declared like this:
// In short,
// Return value *must* be DWORD WINAPI
// And the parameter must be LPVOID
DWORD WINAPI genericThreadFunc1(LPVOID);

// We need an array of Handles to threads
// ...an array of thread id's
// And a waiter (which I'll explain later)
DWORD waiter;
// Here are the three functions that are defined.
// They do trivial things and should be mostly self explanatory.
DWORD WINAPI genericThreadFunc1(LPVOID n)
cout << "Thread started (genericThreadFunc1)..." << endl;
for(int i = 0; i < 100; i++) {
cout << "threadFunc1 says: " << i << endl;
cout << "...(genericThreadFunc1) Thread terminating." << endl;
return (DWORD)n;
cout << "Thread started (printString)..." << endl;
// NOTE: In the next line, we make a pointer and cast what was passed in.
// This is how you use the LPVOID parameters passed into the
// CreateThread call (below).
char* str = (char*)n;
for(int i = 0; i < 50; i++) {
cout << "printString says: " << str << endl;
cout << "...(printString) Thread terminating." << endl;
return (DWORD)n;
int num = (int)n;
for( int i = 2 ; i <= n ; i++)
// isprime = 1;
//for( int j = 2 ; j <= i ; j++)

//if( i == j)
// continue;
//else if( i % j == 0)
//isprime = 0;
cout< }

//cout << "Thread started (printNumber)..." << endl;
//int num = (int)n;
//for (int i = num; i < (num + 100); i++) {
//cout << "printNumber says: " << i << endl;
cout << "...(printHello) Thread terminating." << endl;
return (DWORD)n;

// Get ready, because here's where all the *REAL* magic happens
int main(int argc, char* argv[ ])
int CONSTANT = 2000, isprime;
char myString[20];
strcpy(myString,"Threads are Easy!");
// Here is where we call the CreateThread Win32 API Function that actually
// creates and begins execution of a thread.
// Please read your help files for what each parameter does on
// your Operating system.
// Here's some basics:
// Parameter 0: Lookup
// Parameter 1: Stack size (0 is default which means 1MB)
// Parameter 2: The function to run with this thread
// Parameter 3: Any parameter that you want to pass to the thread function
// Parameter 4: Lookup
// Parameter 5: Once thread is created, an id is put in this variable passed in
hThreads[0] = CreateThread(NULL,0,genericThreadFunc1,(LPVOID)0,NULL,&id[0]);
hThreads[1] = CreateThread(NULL,0,printString,(LPVOID)myString,NULL,&id[1]);
hThreads[2] = CreateThread(NULL,0,printNumber,(LPVOID)CONSTANT,NULL,&id[2]);// Now that all three threads are created and running, we need to stop the primary thread
// (which is this program itself - Remember that once "main" returns, our program exits)
// So that our threads have time to finish. To do this, we do what is called "Blocking".
// We're going to make main just stop and wait until all three threads are done.
// This is done easily with the next line of code. Please read the help file about
// the specific API call "WaitForMultipleObjects".
waiter = WaitForMultipleObjects(MAX_THREADS, hThreads, TRUE, INFINITE);

// After all three threads have finished their task, "main" resumes and we're now ready
// to close the handles of the threads. This is just a bit of clean up work.
// Use the CloseHandle (API) function to do this. (Look it up in the help files as well)
for(int i = 0; i < MAX_THREADS; i++) {
system ("pause");
return 0;

No comments posted yet

Your Answer:

Login to answer
89 Like 43 Dislike
Previous forums Next forums
Other forums

PHP & Java

can PHP code be used inside java code?


Solution Manager BPR
Does the SolMan-BPR-ARIS-Integration (http://www.ids-scheer.com/en/ARIS/ARIS_Platform/ARIS_Business_

Creating Images from images in PHP
Okay well I am trying to make a 'dynamic' calender image with PHP.

I have images like this:

Please help with code for SMTP authenticated PHP email form
Hello, I'm creating a PHP email form, and for this particular server, I have to use SMTP Authenticat

$GPRMC and NMEA how to extract from report
Hi There,

Im a little bi lost and not sure where to start with this one, ive got a small gps

Batch update record with Pagination
Hoping someone can help me with this issue I'm having, im trying to batch update records from a resu

php/mysql auto logout after 2 hour and reset password
Hi all,

This I hope will make sense. I've the following code which when a user logins in, cr

How do I send data using an html link

If I have
<a href="main_file.php">

How do I send data t

restricting another login once you logout

how can I restrict a page from login authenticating against info in a MySQL DB for a s

Using system() and bringing back the results
I am aware that you can use system() within PHP to execute system commands, but I was wondering if t

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS

Play Free Quiz and Win Cash