thread problem...

Discussion in 'C Programming' started by elnanni@gmail.com, Jan 1, 2006.

  1. Guest

    I've a problem in the use of threads, they don't work as i want them
    to.
    Here's the code, the problem is that if i uncomment the
    //pthread_join(thread_ID, NULL);, the main program stops until the
    spawned thread is finished, but that's not the intended way, because i
    need that the opc variable change only when the user pushes a key, i
    think it has something to do with the pthread_attr_set(atribute)
    options, but i didn't get with the answer in the man(3).
    The way i need it works is that main continues looping inside the while
    even if the thread hasn't finished.
    By the way i did't do the mygetch function, but it's not the problem
    with the thread.

    //------------------------------------------------------------
    // This example tries to show the use of pthreadss,
    // it only runs on Linux, because of the pthread.h
    // header file.
    //
    // It has many bugs, you can only run it in a terminal
    // started in a X-session, otherwise it will crash, the main
    // problem is that i commented out the pthread_join line, and
    // i made that because that line waits for the thread to
    // end, and in this example it wouldn't work, i'm almost
    // sure that changing the attr attributes can make this
    // run better, any way, i didn't find the solution, another
    // problem is that the thread runs too fast, so it's
    // difficult to apreciate well the moves of the character.
    // So, if you think you can resolve those problems, please
    // send me a mail, or just modify the code, but please, let
    // know, 'cause i really want to learn the solution.
    //
    // Author: Juan Francisco Benavides Nanni.
    // mail:
    // URL: http://mmabj.tk
    // date: 29/12/2005
    //------------------------------------------------------------

    #include <stdio.h>
    #include <pthread.h>
    #include <termios.h>
    #include <unistd.h>

    #define MAXWIDX 78
    #define MAXWIDY 19
    #define MIN 0
    #define CENTER 10

    #define gotoxy(x,y) printf("\x1B[%i;%iH",(y),(x))
    #define clrscr() printf("\x1B[2J")

    #define UP 115
    #define DOWN 120
    #define LEFT 122
    #define RIG 99
    #define SALE 27

    void mover(int *opc);
    int mygetch();

    int main(){
    pthread_t thread_ID;
    pthread_attr_t attr;
    int opc, last, x, y;
    pthread_attr_init(&attr);
    pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
    pthread_setconcurrency(2);
    char car = '^';
    opc = UP;
    y = CENTER;
    x = y * 4;
    while(1){
    if(opc == SALE) break;
    clrscr();
    switch(opc){
    case UP:
    if(y > MIN) y -= 1;
    else y = MAXWIDY;
    car = '^';
    break;
    case DOWN:
    if(y < MAXWIDY) y += 1;
    else y = MIN;
    car = 'v';
    break;
    case LEFT:
    if(x > MIN) x -= 1;
    else x = MAXWIDX;
    car = '<';
    break;
    case RIG:
    if(x < MAXWIDX) x += 1;
    else x = MIN;
    car = '>';
    break;
    case SALE:
    break;
    default:
    opc = last;
    }
    gotoxy(x, y);
    printf("%c", car);
    last = opc;
    pthread_create(&thread_ID, &attr, (void *) mover, &opc);
    //pthread_join(thread_ID, NULL);
    }
    }

    void mover(int *opc){
    *opc = mygetch();
    }

    int mygetch(){
    struct termios oldt, newt;
    int ch;
    tcgetattr( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    ch = getchar();
    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    return ch;
    }
    , Jan 1, 2006
    #1
    1. Advertising

  2. Jack Klein Guest

    On 31 Dec 2005 20:56:03 -0800, "" <>
    wrote in comp.lang.c:

    > I've a problem in the use of threads, they don't work as i want them
    > to.


    Off-topic here, the C language does not define or support threads.
    Threads are a non-standard extension to the language, and are
    implemented differently on different platforms.

    [snip]

    > //------------------------------------------------------------
    > // This example tries to show the use of pthreadss,
    > // it only runs on Linux, because of the pthread.h
    > // header file.


    [snip]

    The place to ask for help with Linux threads is
    news:comp.os.linux.development.apps. Good luck.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 1, 2006
    #2
    1. Advertising

  3. clayne Guest

    The correct place would be to ask in comp.programming.threads.

    Jack, <pthread.h> refers to POSIX threads, not Linux threads.
    clayne, Jan 1, 2006
    #3
  4. "" <> writes:
    > I've a problem in the use of threads, they don't work as i want them
    > to.


    Standard C doesn't support threads. Try comp.programming.threads.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 1, 2006
    #4
  5. "clayne" <> writes:
    > The correct place would be to ask in comp.programming.threads.
    >
    > Jack, <pthread.h> refers to POSIX threads, not Linux threads.


    Please read <http://cfaj.freeshell.org/google/>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 1, 2006
    #5
  6. clayne Guest

    Your memory that short that you cannot remember the context of my
    statement from within a 5 post thread originating within the same day?
    It's pretty obvious what I was referring to - quoting was unnecessary.
    If anything, the context was completely discernable from what I wrote -
    but keep beating that Google Groups quoting horse... if I sound
    defensive - it's not defense - it's annoyance at unnecessary
    instructing. Get an intelligent news reader, start sorting by thread,
    or mentor people who drop quotes on a 300 post thread, not pointless 5
    post ones. How's that blood pressure coming? :)

    Keith Thompson wrote:
    > "clayne" <> writes:
    > > The correct place would be to ask in comp.programming.threads.
    > >
    > > Jack, <pthread.h> refers to POSIX threads, not Linux threads.

    >
    > Please read <http://cfaj.freeshell.org/google/>.
    clayne, Jan 1, 2006
    #6
  7. clayne said:

    > Your memory that short that you cannot remember the context of my
    > statement from within a 5 post thread originating within the same day?


    Usenet is an asynchronous medium, and comp.lang.c receives hundreds of
    articles a day. What makes you think your contributions are so vital to
    Keith that he should commit to memory the context of every thread in which
    you post an article?

    > if I sound defensive


    You don't. You sound naive. It is a problem that may be cured by education
    and experience, or may not. Those who attempt to educate naive users here
    in comp.lang.c live in hope that it /is/ possible to cure naivete.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jan 1, 2006
    #7
  8. clayne Guest

    Richard Heathfield wrote:

    > Usenet is an asynchronous medium, and comp.lang.c receives hundreds of
    > articles a day. What makes you think your contributions are so vital to
    > Keith that he should commit to memory the context of every thread in which
    > you post an article?


    Newsreader. Sort by thread. Boy that was hard wasn't it? Regardless,
    context was obvious from message.

    > You don't. You sound naive. It is a problem that may be cured by education
    > and experience, or may not. Those who attempt to educate naive users here
    > in comp.lang.c live in hope that it /is/ possible to cure naivete.


    11+ years on the Internet, it's safe for me to say naivete isn't the
    issue. I know when to
    quote and when it's not needed. In the previous case, it was apparent
    what I was
    referring to - the reply I added was only pertinent to those who read
    the thread in
    the first place, i.e. delete it on sight if you didn't. It was the
    pedantic reply on quoting
    that is trumpeting endlessly around here which got on my nerves.

    At the end of the day, get things done, or argue about how we're going
    to get things done? I'd rather just get it done.
    clayne, Jan 1, 2006
    #8
  9. Flash Gordon Guest

    clayne wrote:
    > Richard Heathfield wrote:
    >
    >> Usenet is an asynchronous medium, and comp.lang.c receives hundreds of
    >> articles a day. What makes you think your contributions are so vital to
    >> Keith that he should commit to memory the context of every thread in which
    >> you post an article?

    >
    > Newsreader. Sort by thread. Boy that was hard wasn't it? Regardless,
    > context was obvious from message.


    You still don't understand Usenet. There is absolutely no guarantee that
    the message you are replying to has arrived. How does sorting by thread
    (or anything else) help when you don't HAVE anything to sort?

    >> You don't. You sound naive. It is a problem that may be cured by education
    >> and experience, or may not. Those who attempt to educate naive users here
    >> in comp.lang.c live in hope that it /is/ possible to cure naivete.

    >
    > 11+ years on the Internet, it's safe for me to say naivete isn't the
    > issue.


    It obviously is still a problem, since you assume everyone has seen what
    you have seen.

    > I know when to
    > quote and when it's not needed. In the previous case, it was apparent
    > what I was
    > referring to


    No, it was only apparent to people who happened to have the previous
    article visible.

    > - the reply I added was only pertinent to those who read
    > the thread in
    > the first place, i.e. delete it on sight if you didn't. It was the
    > pedantic reply on quoting
    > that is trumpeting endlessly around here which got on my nerves.


    If you don't like the way thing are meant to be done, i.e. quoting
    properly, then don't hang around in groups where people actually care.

    > At the end of the day, get things done, or argue about how we're going
    > to get things done? I'd rather just get it done.


    Well, since the best way to get things done is to do them properly, do
    them properly and don't argue about it then.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
    Flash Gordon, Jan 1, 2006
    #9
  10. In article <>,
    clayne <> wrote:
    >Richard Heathfield wrote the usual crap:
    >
    >> Usenet is an asynchronous medium, and comp.lang.c receives hundreds of
    >> articles a day. What makes you think your contributions are so vital to
    >> Keith that he should commit to memory the context of every thread in which
    >> you post an article?


    Note that assuming ignorance/inexperience (or pretending to do so) rather
    than admitting the possibility of disagreement (I.e., the earth is either
    round or it isn't) is a standard debaters dirty trick. Gets used around
    here a lot.

    >Newsreader. Sort by thread. Boy that was hard wasn't it? Regardless,
    >context was obvious from message.


    Sing it, sister!

    >> You don't. You sound naive. It is a problem that may be cured by education
    >> and experience, or may not. Those who attempt to educate naive users here
    >> in comp.lang.c live in hope that it /is/ possible to cure naivete.

    >
    >11+ years on the Internet, it's safe for me to say naivete isn't the
    >issue. I know when to quote and when it's not needed. In the previous
    >case, it was apparent what I was referring to - the reply I added was only
    >pertinent to those who read the thread in the first place, i.e. delete it
    >on sight if you didn't. It was the pedantic reply on quoting that is
    >trumpeting endlessly around here which got on my nerves.


    Yes, they are just beating a dead horse. Pretending that the Usenet hasn't
    changed at all in the last 15 years.

    Most of these guys are so old that 15 years is nothing to them.

    >At the end of the day, get things done, or argue about how we're going
    >to get things done? I'd rather just get it done.


    Then you are clearly in the wrong newsgroup...
    Kenny McCormack, Jan 1, 2006
    #10
  11. Kenny McCormack said:

    > In article <>,
    > clayne <> wrote:
    >>Richard Heathfield wrote the usual crap:
    >>
    >>> Usenet is an asynchronous medium, and comp.lang.c receives hundreds of
    >>> articles a day. What makes you think your contributions are so vital to
    >>> Keith that he should commit to memory the context of every thread in
    >>> which you post an article?

    >
    > Note that assuming ignorance/inexperience (or pretending to do so) rather
    > than admitting the possibility of disagreement (I.e., the earth is either
    > round or it isn't) is a standard debaters dirty trick.


    Whether the earth is round depends on your definition of "round", and there
    was no dirty-trick debating in the text you quoted above.


    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Jan 1, 2006
    #11
  12. Chuck F. Guest

    *** rude topposting of rude material corrected ***

    clayne wrote:
    > Keith Thompson wrote:
    >> "clayne" <> writes:

    >
    >>> The correct place would be to ask in
    >>> comp.programming.threads.
    >>>
    >>> Jack, <pthread.h> refers to POSIX threads, not Linux
    >>> threads.

    >>
    >> Please read <http://cfaj.freeshell.org/google/>.

    >
    > Your memory that short that you cannot remember the context of
    > my statement from within a 5 post thread originating within the
    > same day? It's pretty obvious what I was referring to - quoting
    > was unnecessary. If anything, the context was completely
    > discernable from what I wrote - but keep beating that Google
    > Groups quoting horse... if I sound defensive - it's not defense
    > - it's annoyance at unnecessary instructing. Get an intelligent
    > news reader, start sorting by thread, or mentor people who drop
    > quotes on a 300 post thread, not pointless 5 post ones. How's
    > that blood pressure coming? :)


    FYI, yes. Some of us are senile and read several hundred posts
    daily. Our readers automatically destroy or hide read posts.

    Failure to include *clipped* context, and topposting, are both evil
    sins, which foul up communications in general. This is why the
    google interface to Usenet is so appalling.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Chuck F., Jan 1, 2006
    #12
  13. In article <dp8rbj$g01$-infra.bt.com>,
    Richard Heathfield <> wrote:
    >Kenny McCormack said:
    >
    >> In article <>,
    >> clayne <> wrote:
    >>>Richard Heathfield wrote the usual crap:
    >>>
    >>>> Usenet is an asynchronous medium, and comp.lang.c receives hundreds of
    >>>> articles a day. What makes you think your contributions are so vital to
    >>>> Keith that he should commit to memory the context of every thread in
    >>>> which you post an article?

    >>
    >> Note that assuming ignorance/inexperience (or pretending to do so) rather
    >> than admitting the possibility of disagreement (I.e., the earth is either
    >> round or it isn't) is a standard debaters dirty trick.

    >
    >Whether the earth is round depends on your definition of "round", and there
    >was no dirty-trick debating in the text you quoted above.


    I could assert that 2+2=4 and you'd disagree.

    Note to observers: I'm not kidding. I fully expect a post from him within
    the hour "proving" me wrong.
    Kenny McCormack, Jan 1, 2006
    #13
  14. Malcolm Guest

    "Kenny McCormack" <> wrote
    > Note that assuming ignorance/inexperience (or pretending to do so) rather
    > than admitting the possibility of disagreement (I.e., the earth is either
    > round or it isn't) is a standard debaters dirty trick. Gets used around
    > here a lot.
    >

    Your newreader added a spurious capital to the last sentence. It happens to
    the best of us - autocorrection is often a nuisance.
    Malcolm, Jan 1, 2006
    #14
  15. Randy Howard Guest

    Jack Klein wrote
    (in article <>):

    > The place to ask for help with Linux threads is
    > news:comp.os.linux.development.apps. Good luck.


    Actually, comp.programming.threads is probably a more focused
    group to ask such a question, not to mention one or more members
    of the POSIX committee that came up with pthreads are known to
    subscribe.


    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
    Randy Howard, Jan 1, 2006
    #15
  16. Richard Heathfield <> writes:
    > Kenny McCormack said:

    [snip]
    > Whether the earth is round depends on your definition of "round", and there
    > was no dirty-trick debating in the text you quoted above.


    Richard, *please* don't feed the troll. clayne may still be educable;
    it's far past time to realize that Kenny isn't and never will be.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Jan 1, 2006
    #16
  17. Jack Klein Guest

    On 31 Dec 2005 22:11:29 -0800, "clayne" <> wrote in
    comp.lang.c:

    > The correct place would be to ask in comp.programming.threads.


    The correct place to ask WHAT??

    This group is littered with instructions on how to quote properly,
    even if you are using the idiot broken Google groups interface. Like
    Microsoft before it, Google is not going to succeed in redefining
    email and usenet etiquette at its whim.

    > Jack, <pthread.h> refers to POSIX threads, not Linux threads.


    Part of the OP's original post, which I quoted and you failed to
    quote, along with everything else in my reply, was:

    > // This example tries to show the use of pthreadss,
    > // it only runs on Linux, because of the pthread.h
    > // header file.


    Kindly read the three lines above, carefully. Especially the middle
    one. Perhaps the OP's Linux distro (and which one is it, with which
    kernel?) implements POSIX threads in a 100% conforming manner. Perhaps
    not, I've seen quite a few failures of true POSIX conformance in
    various Linux distributions over the years.

    Note that the C language doesn't know or care whether Linux pthreads
    are POSIX conforming. The OP asked for help with a program that in
    his own words he wants to run only on Linux, the proper redirection is
    to news:comp.os.linux.development.apps, just as I said.

    If the Linux application experts think he would be better off in
    news:comp.programming.threads, they will tell him so.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jan 2, 2006
    #17
  18. Guest

    Well, thanks anyway to all of you, but i finally found the answer,
    thanks to this book
    http://www.advancedlinuxprogramming.com/alp-folder/alp-ch04-threads.pdf,
    instead of the commented line //pthread_join(thread_ID, NULL);, i use
    this one: pthread_detach(thread_ID); and i changed the const MAXWIDY to
    78, and that's all, now it works good, by the way, sorry for the
    off-topic.
    , Jan 2, 2006
    #18
  19. In article <>,
    Keith Thompson <> wrote:
    >Richard Heathfield <> writes:
    >> Kenny McCormack said:

    >[snip]
    >> Whether the earth is round depends on your definition of "round", and there
    >> was no dirty-trick debating in the text you quoted above.

    >
    >Richard, *please* don't feed the troll. clayne may still be educable;
    >it's far past time to realize that Kenny isn't and never will be.


    See what I mean about these twerps endlessly pretending to assume lack of
    education or experience (in their enemies), when the fact is that they (the
    twerps) are just plain wrong?
    Kenny McCormack, Jan 2, 2006
    #19
  20. Malcolm Guest

    "Kenny McCormack" <> wrote
    >
    >>Richard, *please* don't feed the troll. clayne may still be educable;
    >>it's far past time to realize that Kenny isn't and never will be.

    >
    > See what I mean about these twerps endlessly pretending to assume lack of
    > education or experience (in their enemies), when the fact is that they
    > (the
    > twerps) are just plain wrong?
    >

    I did think that maybe Keith was being too harsh, but I now see that he was
    right.

    Bye bye.
    Malcolm, Jan 2, 2006
    #20
    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,539
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. Manisha
    Replies:
    2
    Views:
    529
  3. NaeiKinDus
    Replies:
    1
    Views:
    567
    Jack Klein
    Apr 14, 2007
  4. NaeiKinDus
    Replies:
    3
    Views:
    600
    James Kanze
    Apr 15, 2007
  5. Replies:
    3
    Views:
    157
Loading...

Share This Page