Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > thread problem...

Reply
Thread Tools

thread problem...

 
 
elnanni@gmail.com
Guest
Posts: n/a
 
      01-01-2006
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;
}

 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-01-2006
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/~a...FAQ-acllc.html
 
Reply With Quote
 
 
 
 
clayne
Guest
Posts: n/a
 
      01-01-2006
The correct place would be to ask in comp.programming.threads.

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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-01-2006
"" <> 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) kst- <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-01-2006
"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) kst- <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.
 
Reply With Quote
 
clayne
Guest
Posts: n/a
 
      01-01-2006
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/>.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-01-2006
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)
 
Reply With Quote
 
clayne
Guest
Posts: n/a
 
      01-01-2006
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.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-01-2006
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.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      01-01-2006
In article < .com>,
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...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminating a thread from the main thread Charles A. Lackman ASP .Net 3 12-09-2004 02:12 PM
Thread was being aborted thrown for background thread (win2003 ser =?Utf-8?B?Sm9oYW5uYQ==?= ASP .Net 3 10-15-2004 01:35 PM
Thread was being aborted in win2003 server. Back ground thread reading MS access database, no redirects or transfers. Johanna ASP .Net 0 10-13-2004 01:32 PM
"Thread was being aborted" error from WebApp using Thread.Sleep. Stephen Miller ASP .Net 3 07-01-2004 11:50 PM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57