Like Tree1Likes
  • 1 Post By MK27

Simple multithreading problem

This is a discussion on Simple multithreading problem within the C Programming forums, part of the General Programming Boards category; Hi. I can't seem to figure out why my thread doesn't show the value 10. I get some 7 digit ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    Simple multithreading problem

    Hi.

    I can't seem to figure out why my thread doesn't show the value 10. I get some 7 digit number instead.
    Can anyone see the error?

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    void ThreadProc(void *param);
    
    int main()
    {
        int val = 10;
    
        HANDLE handle = (HANDLE) _beginthread( ThreadProc,0,&val); // create thread
        WaitForSingleObject(handle,INFINITE);
    
        return 0;
    }
    
    void ThreadProc(void *param)
    {
        int *h =&param;
        printf("%d\n", h);
        _endthread();
    
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by praised View Post
    Code:
    void ThreadProc(void *param)
    {
        int *h = param;
        printf("%d\n", *h);
        _endthread();
    
    }
    You want the value, not the pointer to pointer.
    Last edited by AndrewHunter; 08-14-2011 at 11:24 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    You are handling your void pointer wrong.
    Code:
    void ThreadProc(void *param)
    {
        int *h =(int *)param;
        printf("%d\n", *h);
        _endthread();
    }
    Plus I "believe" you are supposed to typecast the void pointer to an int pointer.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by carrotcake1029 View Post
    Plus I "believe" you are supposed to typecast the void pointer to an int pointer.
    Not required. Per the standard, a void pointer can be assigned to any pointer type, except function pointers, safely and without the need for a cast. That is why you do not have to cast the return of malloc.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    2
    Thanks for the quick replies.

    However, I'm not sure the just getting the value of it would do. Further down the road I'd like to be able to change the value of val through ThreadProc.
    Got any suggestions of how this could be done?

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by praised View Post
    However, I'm not sure the just getting the value of it would do.
    Well, then you should have asked for something else then.

    Quote Originally Posted by praised View Post
    Further down the road I'd like to be able to change the value of val through ThreadProc.
    Got any suggestions of how this could be done?
    Then you would need a pointer to a pointer. Take a look at Lesson 6-Pointers and Prelude's Pointer Tutorial. That should give you all the knowledge you need. BTW, this has nothing to do with multithreading. I would suggest you get this working without multithreading first, and then go onto the next step.

    EDIT: Having multiple threads access data members can cause a lot of problems if not designed correctly. You may find some value with adrian's tutorials. Just scroll down until you see the final three tutorials on multithreading.
    Last edited by AndrewHunter; 08-14-2011 at 11:47 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Eh, I don't think a pointer to a pointer is needed here; val is an int, so to change val you just need a pointer-to-int, which is conveniently what he has.

    As Andrew says, multithreading is a distraction here -- get it to work with a regular program first; passing things back and forth between functions works the same with one thread or a thousand.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by tabstop View Post
    Eh, I don't think a pointer to a pointer is needed here; val is an int, so to change val you just need a pointer-to-int, which is conveniently what he has.
    Thanks for the correction tabstop. Typing too fast for my brain again.

    EDIT: @OP:

    Code:
    #include <stdio.h>
    
    void procedure(void *);
    
    int main(void){
    
    	int x = 10;
    
    	procedure(&x);
    	printf("\n%d", x);
    
    	getchar();
    	return(0);
    }
    void procedure(void *y){
    
    	int* z = y;
    	printf("%d", *z);
    	*z = 5;
    }
    Last edited by AndrewHunter; 08-14-2011 at 12:04 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by praised View Post
    Thanks for the quick replies.

    However, I'm not sure the just getting the value of it would do. Further down the road I'd like to be able to change the value of val through ThreadProc.
    Got any suggestions of how this could be done?
    You don't have just the value. Andrew was simply saying that if you were trying to print the value of the int pointed to by *h -- which it certainly looks like that's what you were trying to do:

    Code:
    void ThreadProc(void *param)
    {
        int *h =&param;   // wrong
        printf("%d\n", h);  // wrong
        _endthread();
    
    }
    Then you were doing it wrong. That would print out the address of the pointer, in which case you should be using %p, not %d. Also, you assigned *h to &param, but param is already a pointer. Lose the address of operator.

    You can assign a value to *h and it will be reflected in the calling thread/function/other threads with the same pointer.

    Code:
    *h = 666;
    HOWEVER, WRT TO THREADING, DO NOT DO THAT NAIVELY. You must use mutexes or some other form of locking. Threads do not guarantee the integrity of the value otherwise, and it is possible, eg, for one thread to get a partial read of an integer value while another thread does a partial write, creating a garbage value.

    If you think you are going to have other variables you want accessed in the thread, pass a struct. This also simplifies the using of pointers and casts, etc, because you do not need a pointer to every variable, just a pointer to the struct where they reside.
    Last edited by MK27; 08-14-2011 at 12:09 PM.
    AndrewHunter likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by carrotcake1029 View Post
    You are handling your void pointer wrong.
    Code:
    void ThreadProc(void *param)
    {
        int *h =(int *)param;
        printf("%d\n", *h);
        _endthread();
    }
    Plus I "believe" you are supposed to typecast the void pointer to an int pointer.
    Actually.... it's ... int h = *param ... but who's counting?

  11. #11
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    You can dereference a void pointer now? Who knew

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    Actually.... it's ... int h = *param ... but who's counting?
    Nope. As adeyblue hints, that will lead to a compiler error, unless you use a cast of the form int h = *(int*)param.

    But why would you recommend this at all when the OP has said explicitly that s/he wants a pointer?

    int *h = param is correct.
    Last edited by MK27; 08-14-2011 at 02:11 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading, tbb, local variable problem
    By idleman in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2009, 03:30 PM
  2. Help with simple multithreading.
    By Blackroot in forum C++ Programming
    Replies: 15
    Last Post: 02-03-2006, 08:02 AM
  3. Multithreading problem
    By Bacardi34 in forum C Programming
    Replies: 5
    Last Post: 09-02-2004, 03:26 PM
  4. Multithreading & Problem :: MFC
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 06-06-2002, 01:33 PM
  5. Interesting Multithreading Problem :: MFC
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 04-22-2002, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21