|
-
June 30th, 2004, 04:01 AM
#1
Multithreading problem... can anyone solve?
Hi !
I am totally new to multithreading. I know some theories behind but I don't have actual coding experience. Can anyone solve the problem below?
Problem:
Write a program that will increment and display an unsigned integer (UINT) for every 100 milliseconds.
UINT must start from 0 then will be reset to 0 when the highest value that a UINT can have is reached.
The program will terminate when the user presses any key.
CONDITIONS:
1. Create a worker thread that will increment and display the integer.
2. When the user presses any key, the worker thread must be terminated, then the program exits.
Thanks!
-
June 30th, 2004, 04:14 AM
#2
maybe it 's your homework?
if not ,i suggest you use the timer instead of the thread
-
June 30th, 2004, 04:23 AM
#3
Re: Multithreading problem... can anyone solve?
Originally posted by jbp2004
Hi !
I am totally new to multithreading. I know some theories behind but I don't have actual coding experience. Can anyone solve the problem below?
Problem:
Write a program that will increment and display an unsigned integer (UINT) for every 100 milliseconds.
UINT must start from 0 then will be reset to 0 when the highest value that a UINT can have is reached.
The program will terminate when the user presses any key.
CONDITIONS:
1. Create a worker thread that will increment and display the integer.
2. When the user presses any key, the worker thread must be terminated, then the program exits.
Thanks!
search in this FAQ for Threads
-
June 30th, 2004, 04:30 AM
#4
-
June 30th, 2004, 06:46 PM
#5
It could be a homework, yes :-) I am undergoing training on C programming. I have finished the basics including advance topics like pointers, arrays, and file handling. Now, I'm on INTERPROCESS COMMUNICATION (IPC), the problem is, it is my first time to face this aspect of C programming. :-)
Thanks for your replies.
-
July 1st, 2004, 02:21 AM
#6
Okay...so you are using C? Furthermore...interprocess communication deals with different processes not threads...
-
July 2nd, 2004, 05:18 PM
#7
this homework assignment is stupid easy... ok grasshopper... look up two things... how to create, access, and change a pointer, and the subject variable... in this case, an unsigned int. now look up the spawn functions in whatever compiler you are using, see what they do. this is homework, so do it. write back.
-
July 3rd, 2004, 09:38 PM
#8
-
July 5th, 2004, 12:49 AM
#9
I made the answer to my own question :-)
Code:
//=============================================================
// Project : DisplayUINT
// OS : Windows
// File : DisplayUINT.c
// Version : 1.0
// Description : A porgram that display value of a UINT variable
// starting from zero to maximum value of UINT
// When UINT_MAX is reached, value is set back to 0
// History : 2004-07-05 Created by jbp2004
//=============================================================
#include <windows.h> /* create thread function */
#include <stdio.h>
#include <conio.h> /* _kbhit() function */
//=====================================================================
// Function : PrintVal
// Description : This function is executed by the thread created using
// CreateThread function. It receives the current value of
// uiVal, display this value and increment by 1.
// Argument : void *param - accepts the value of uiVal
// Return : void
// Note :
// History : 2004-07-05 Created by jbp2004
//=====================================================================
void PrintVal(void *param) {
UINT *nVal = (UINT *)param; /* assigns the value pointed to by param to nVal */
/* loop while contion in main function is true */
while (1) {
printf("%5d",*nVal); /* print the current value */
_sleep(100); /* 100 ms time interval */
(*nVal)++; /* then increment */
/* check if maximum value is reached then reset to 0 */
if ((*nVal)==4294967295)
*nVal = 0;
}
}
void main() {
UINT uiVal=0;
/* created thread to execute statements in PrintVal with pointer to
security attributes set to NULL, stack size set to 0, PrintVal argument
current value of uiVal, creation flag set to 0 (so that the thread is not
suspended), and thread id is set to NULL */
if ((CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)PrintVal,&uiVal,0,NULL))==NULL) {
printf("\nCreate thread failed.\n");
return;
}
while (!_kbhit()); /* thread created continue to display and increment number as long
as no key is hit or pressed from the keyboard */
}
Last edited by jbp2004; July 5th, 2004 at 07:31 PM.
-
February 22nd, 2005, 03:35 PM
#10
Re: Multithreading problem... can anyone solve?
Nice work jbp2004 !
Glad you solved your own problem
-
February 22nd, 2005, 04:54 PM
#11
Re: Multithreading problem... can anyone solve?
>> History : 2004-07-05 Created by jbp2004
If you solved this in July of last year, why are you asking the question now?
Arjay
-
February 25th, 2005, 01:04 PM
#12
Re: Multithreading problem... can anyone solve?
He's not. Look at the date of the post.
-
February 25th, 2005, 01:57 PM
#13
Re: Multithreading problem... can anyone solve?
 Originally Posted by JimG
He's not. Look at the date of the post.
Are you sure? The comments are dated for last year.
Code:
//=============================================================
// Project : DisplayUINT
// OS : Windows
// File : DisplayUINT.c
// Version : 1.0
// Description : A porgram that display value of a UINT variable
// starting from zero to maximum value of UINT
// When UINT_MAX is reached, value is set back to 0
// History : 2004-07-05 Created by jbp2004
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|