none
the child thread blocks the parent thread.

    Question

  • i don't figure out it.

    #include <iostream>
    #include <vector>
    #include <windows.h>

    using namespace std;
    void __cdecl Thread(PVOID pvParam)
    {
     int a = *(int*) pvParam;
     while ( 1 )
     {
      a ++;
      Sleep(1000);
     }
    }

    void __cdecl ThreadDis(PVOID pvParam)
    {
     while ( 1 )
     {
      cout<<*(int*)pvParam<<endl;  //when it executes
     }

    }

    void func1()
    {
     static int c = 1;
     Thread(&c);
     cout<<c<<endl;   //it will not execute even in a long time
    }

    void func2()
    {
     static int c;
     c = 6;
     ThreadDis(&c);
    }

    void main()
    {
     static int c;
     func1();
     func2();
     cout<<c<<endl;

    }

     

    Tuesday, April 14, 2009 6:04 AM

Answers

  • If you are saying the static int passed in isn't changing in the parent, it is because you are taking a copy of the int:

     int a = *(int*) pvParam;

    Instead take a pointer to it (or reference)

     int* pa = (int*) pvParam;

    (*pa)++;

    Is this what you mean?

    Actually looking at it again, you seem to have different static ints all over the place, try this instead:

    void main()
    {
     static int c;
     func1(c);
     func2(c);
     cout<<c<<endl;

    }

    void func1(int& c)

    {
     Thread(&c); << - This will block

    // _beginthread(Thread,0,(void*)&c); << use this instead, none blocking

      cout << "func1" <<c<<endl;   //it will not execute even in a long time  

    }

    void func2(int& c)
    {
     c = 6;
     ThreadDis(&c);
    }

    void __cdecl Thread(PVOID pvParam)
    {
     int* pa = (int*) pvParam;
     while ( 1 )
     {
      (*pa)++;
      Sleep(1000);
     }

    //this will never exit
    }

     

    void __cdecl ThreadDis(PVOID pvParam)
    {
     while ( 1 )
     {
      cout<<(*(int*)pvParam)<<endl;  //when it executes
     }

    //this will never exit

    }

     

    Note: Its early on the first day back after hols, so I'm half asleep still ;)

    • Edited by Mark Duffill Tuesday, April 14, 2009 8:45 AM it was wrong
    • Marked as answer by cutedevil Tuesday, April 14, 2009 9:52 AM
    Tuesday, April 14, 2009 8:26 AM

All replies

  • If you are saying the static int passed in isn't changing in the parent, it is because you are taking a copy of the int:

     int a = *(int*) pvParam;

    Instead take a pointer to it (or reference)

     int* pa = (int*) pvParam;

    (*pa)++;

    Is this what you mean?

    Actually looking at it again, you seem to have different static ints all over the place, try this instead:

    void main()
    {
     static int c;
     func1(c);
     func2(c);
     cout<<c<<endl;

    }

    void func1(int& c)

    {
     Thread(&c); << - This will block

    // _beginthread(Thread,0,(void*)&c); << use this instead, none blocking

      cout << "func1" <<c<<endl;   //it will not execute even in a long time  

    }

    void func2(int& c)
    {
     c = 6;
     ThreadDis(&c);
    }

    void __cdecl Thread(PVOID pvParam)
    {
     int* pa = (int*) pvParam;
     while ( 1 )
     {
      (*pa)++;
      Sleep(1000);
     }

    //this will never exit
    }

     

    void __cdecl ThreadDis(PVOID pvParam)
    {
     while ( 1 )
     {
      cout<<(*(int*)pvParam)<<endl;  //when it executes
     }

    //this will never exit

    }

     

    Note: Its early on the first day back after hols, so I'm half asleep still ;)

    • Edited by Mark Duffill Tuesday, April 14, 2009 8:45 AM it was wrong
    • Marked as answer by cutedevil Tuesday, April 14, 2009 9:52 AM
    Tuesday, April 14, 2009 8:26 AM
  • "the child thread blocks the parent thread"

    I do not see any threads in your post.
    David Wilkinson | Visual C++ MVP
    Tuesday, April 14, 2009 11:28 AM