Thread program

Discussion in 'C++' started by takeshi honda, Nov 3, 2012.

  1. I'm trying to create a thread program according to this article.
    http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

    The sample code is a case that each thread processes easy quick tasks.
    In this case I have no problem.
    But I want two threads to do time consuming process such as recvfrom().
    In this case threading program doesn't work well.

    while(1){
    thread1.Event(); // this is waiting recvfrom() return.
    thread2.Event(); // this doesn't start until above process finishes.
    }

    I also tried ThreadTypeIntervalDriven as follows.

    thread1.SetThreadType(ThreadTypeIntervalDriven,10);
    thread2.SetThreadType(ThreadTypeIntervalDriven,10);

    For some reason, this causes a crash of the program.

    How can I create program which has two threads using recvfrom()?
    takeshi honda, Nov 3, 2012
    #1
    1. Advertising

  2. takeshi honda

    Robert Miles Guest

    On Saturday, November 3, 2012 8:51:25 AM UTC-5, takeshi honda wrote:
    > I'm trying to create a thread program according to this article.
    >
    > http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class
    >
    > The sample code is a case that each thread processes easy quick tasks.
    >
    > In this case I have no problem.
    >
    > But I want two threads to do time consuming process such as recvfrom().
    >
    > In this case threading program doesn't work well.
    >
    >
    > while(1){
    > thread1.Event(); // this is waiting recvfrom() return.
    > thread2.Event(); // this doesn't start until above process finishes.
    > }
    >
    >
    > I also tried ThreadTypeIntervalDriven as follows.
    >
    >
    > thread1.SetThreadType(ThreadTypeIntervalDriven,10);
    > thread2.SetThreadType(ThreadTypeIntervalDriven,10);
    >
    >
    > For some reason, this causes a crash of the program.
    >
    > How can I create program which has two threads using recvfrom()?


    There's another newsgroup, comp.programming.threads, which may be appropriate
    for this question.
    Robert Miles, Nov 3, 2012
    #2
    1. Advertising

  3. takeshi honda

    Jorgen Grahn Guest

    On Sat, 2012-11-03, Luca Risolia wrote:
    > On 03/11/2012 14:51, takeshi honda wrote:
    >> I'm trying to create a thread program according to this article.
    >> http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

    >
    > Have you ever had a look at the C++11 standard for threads? That article
    > seems obsolete.


    Not so much because it doesn't rely on the C++11 standard -- you can't
    expect everyone to use it already. But the code seems archaic at a
    glance (BOOL, CThread, no namespace ...) so I'm skeptic too.

    Surely there are better pre-C++11 examples out there. Boost?

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 3, 2012
    #3
  4. takeshi honda

    Öö Tiib Guest

    On Saturday, 3 November 2012 15:51:25 UTC+2, takeshi honda wrote:
    > I'm trying to create a thread program according to this article.
    > http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class


    I do not like the implementation at all. Reasons:
    * No namespaces, all that crap goes to global namespace
    * Naming conventions especially Hungarian crap like "m_dwThread" and even both prefix and verbose
    telling what it is like "CMutexClass"
    * No separate RAII Lock class instead there are separate Lock and Unlock methods of mutex, so no exception safety.
    * etc.

    You will learn bad habits from that code, delete it.
    If your compiler has std::thread then use it.
    If it does not have download boost and use boost::thread.

    > How can I create program which has two threads using recvfrom()?


    What you seem to need for that is actually boost::asio ... learn to use it.
    Öö Tiib, Nov 3, 2012
    #4
  5. takeshi honda

    Paul N Guest

    On Nov 3, 5:12 pm, Öö Tiib <> wrote:
    > On Saturday, 3 November 2012 15:51:25 UTC+2, takeshi honda  wrote:
    > > I'm trying to create a thread program according to this article.
    > >http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

    >
    > I do not like the implementation at all. Reasons:
    > * No namespaces, all that crap goes to global namespace
    > * Naming conventions especially Hungarian crap like  "m_dwThread" and even both prefix and verbose
    >   telling what it is like  "CMutexClass"


    Sorry if this is a naive question, but - I thought the point of
    namespaces was to avoid name clashes, so why does it matter that the
    names are in the global namespace if they're horrible ones which you
    presumably aren't going to want to use yourself?
    Paul N, Nov 4, 2012
    #5
  6. takeshi honda

    Ian Collins Guest

    On 11/05/12 10:40, Paul N wrote:
    > On Nov 3, 5:12 pm, Öö Tiib<> wrote:
    >> On Saturday, 3 November 2012 15:51:25 UTC+2, takeshi honda wrote:
    >>> I'm trying to create a thread program according to this article.
    >>> http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

    >>
    >> I do not like the implementation at all. Reasons:
    >> * No namespaces, all that crap goes to global namespace
    >> * Naming conventions especially Hungarian crap like "m_dwThread" and even both prefix and verbose
    >> telling what it is like "CMutexClass"

    >
    > Sorry if this is a naive question, but - I thought the point of
    > namespaces was to avoid name clashes, so why does it matter that the
    > names are in the global namespace if they're horrible ones which you
    > presumably aren't going to want to use yourself?


    Some other library may be foolish enough to use them?

    I agree the code looks like some awful 90s MFC mess.

    --
    Ian Collins
    Ian Collins, Nov 4, 2012
    #6
  7. takeshi honda

    Jorgen Grahn Guest

    On Sun, 2012-11-04, Paul N wrote:
    > On Nov 3, 5:12 pm, Öö Tiib <> wrote:
    >> On Saturday, 3 November 2012 15:51:25 UTC+2, takeshi honda  wrote:
    >> > I'm trying to create a thread program according to this article.
    >> >http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class

    >>
    >> I do not like the implementation at all. Reasons:
    >> * No namespaces, all that crap goes to global namespace
    >> * Naming conventions especially Hungarian crap like  "m_dwThread"
    >> and even both prefix and verbose
    >>   telling what it is like  "CMutexClass"

    [several other reasons snipped by PN]

    > Sorry if this is a naive question, but - I thought the point of
    > namespaces was to avoid name clashes, so why does it matter that the
    > names are in the global namespace if they're horrible ones which you
    > presumably aren't going to want to use yourself?


    You mean it's ok to skip namespaces if you choose really awful names
    for everything?

    Apart from that: namespaces aren't just for avoiding name clashes.
    They also /group/ related names.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Nov 4, 2012
    #7
  8. takeshi honda

    Guest

    On Sunday, November 4, 2012 5:46:02 PM UTC-5, Paavo Helde wrote:
    >
    > I gather you are kind of flagellant person wanting to invent and use
    >
    > horrible ugly names in your everyday work. Unfortunately, most people are
    >
    > more comfortable and would like to use normal meaningful names instead.
    >
    > For example, the above link contains code for a class named CTask, which
    >
    > is not so horrible (sans the meaningless C prefix) and has a real danger
    >
    > of colliding with a name in another library of a similar mindset.
    >
    >
    >
    > Namespaces solve this dilemma quite neatly. You just put everything in a
    >
    > namespace which is unique/ugly enough and use short meaningful names
    >
    > inside the implementation in the namespace; in other code using this
    >
    > library you can use short namespace aliases or using directives
    >
    > (discouraged). E.g:



    Is there a need for an on line registry of namespaces?



    Brian
    Ebenezer Enterprises -- making programming fun again.
    http://webEbenezer.net
    , Nov 5, 2012
    #8
  9. Thank you everyone.

    Unfortunately, I do program in severe environment where neither std::threadnor boost exists. I am not an admin of the server.

    Do you have any idea for this solution?

    2012年11月3日土曜日 22時51分25秒 UTC+9 takeshi honda:
    > I'm trying to create a thread program according to this article.
    >
    > http://www.codeproject.com/Articles/21114/Creating-a-C-Thread-Class
    >
    >
    >
    > The sample code is a case that each thread processes easy quick tasks.
    >
    > In this case I have no problem.
    >
    > But I want two threads to do time consuming process such as recvfrom().
    >
    > In this case threading program doesn't work well.
    >
    >
    >
    > while(1){
    >
    > thread1.Event(); // this is waiting recvfrom() return.
    >
    > thread2.Event(); // this doesn't start until above process finishes.
    >
    > }
    >
    >
    >
    > I also tried ThreadTypeIntervalDriven as follows.
    >
    >
    >
    > thread1.SetThreadType(ThreadTypeIntervalDriven,10);
    >
    > thread2.SetThreadType(ThreadTypeIntervalDriven,10);
    >
    >
    >
    > For some reason, this causes a crash of the program.
    >
    >
    >
    > How can I create program which has two threads using recvfrom()?
    takeshi honda, Nov 5, 2012
    #9
  10. takeshi honda

    Guest

    On Monday, November 5, 2012 12:00:19 AM UTC-6, Paavo Helde wrote:
    >
    > No, we do not need another ICANN, if that's what you mean. If at worst all
    >
    > 7G people suddenly started to code in C++, then we could just insert UUIDs
    >
    > in all namespace names.
    >
    >


    I don't like the idea of adding UUID to names.
    Names should be meaningful.
    , Nov 6, 2012
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,541
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    490
  3. Stephen Miller
    Replies:
    3
    Views:
    3,966
    Stephen Miller
    Jul 2, 2004
  4. Jeffrey Barish
    Replies:
    0
    Views:
    302
    Jeffrey Barish
    May 28, 2004
  5. liu yang
    Replies:
    4
    Views:
    2,005
    Antoninus Twink
    Jul 28, 2008
Loading...

Share This Page