thread problem...

E

elnanni

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: (e-mail address removed)
// 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;
}
 
J

Jack Klein

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
Good luck.
 
C

clayne

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

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

clayne

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? :)
 
R

Richard Heathfield

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.
 
C

clayne

Richard said:
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.
 
F

Flash Gordon

clayne said:
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?
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.
 
K

Kenny McCormack

Richard Heathfield wrote the usual crap:

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!
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...
 
R

Richard Heathfield

Kenny McCormack said:
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.
 
C

Chuck F.

*** rude topposting of rude material corrected ***
> 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/>
 
K

Kenny McCormack

Kenny McCormack said:


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.
 
M

Malcolm

Kenny McCormack said:
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.
 
R

Randy Howard

Jack Klein wrote
(in article said:
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.
 
K

Keith Thompson

Richard Heathfield said:
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.
 
J

Jack Klein

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 just as I said.

If the Linux application experts think he would be better off in
they will tell him so.
 
K

Kenny McCormack

Richard Heathfield said:
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?
 
M

Malcolm

Kenny McCormack said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top