CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    31

    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!

  2. #2
    Join Date
    Jun 2004
    Posts
    12
    maybe it 's your homework?
    if not ,i suggest you use the timer instead of the thread

  3. #3
    Join Date
    Feb 2002
    Posts
    3,788

    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

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  5. #5
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    31
    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.

  6. #6
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    Okay...so you are using C? Furthermore...interprocess communication deals with different processes not threads...

  7. #7
    Join Date
    Jul 2004
    Posts
    1
    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.

  8. #8
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    31
    ok, delete this thread.

  9. #9
    Join Date
    Jun 2004
    Location
    Philippines
    Posts
    31
    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.

  10. #10
    Join Date
    Feb 2005
    Posts
    7

    Re: Multithreading problem... can anyone solve?

    Nice work jbp2004 !
    Glad you solved your own problem

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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

  12. #12
    Join Date
    Apr 2004
    Location
    Tucson, Az
    Posts
    48

    Re: Multithreading problem... can anyone solve?

    He's not. Look at the date of the post.

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Multithreading problem... can anyone solve?

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured