473,803 Members | 3,416 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

thread problem...

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(th read_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_se t(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: el*****@gmail.c om
// 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_in it(&attr);
pthread_attr_se tscope(&attr, PTHREAD_SCOPE_P ROCESS);
pthread_setconc urrency(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(th read_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 '06
39 1880
nelu said:

Kenny McCormack wrote:
You, also, need to get some help in learning to distinguish between
Usenet and real life.


Even when a lot of people apply what they learn on Usenet in real-life?


You need to learn to distinguish between Kenny McCormack and a person
genuinely interested in, and capable of learning from, your answer.

--
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)
Jan 3 '06 #31
ga*****@yin.int eraccess.com (Kenny McCormack) writes:
In article <dp**********@n wrdmz02.dmz.ncs .ea.ibs-infra.bt.com>,
Richard Heathfield <in*****@invali d.invalid> wrote:
(The usual RH psychobabble - deleted)
(Then clayne says)
But hey, it's that important to be right, right?


It can be, yes, when lives are at stake. Badly-written code can kill people.

[snip]

_______________ ____
/| /| | |
||__|| | Please do |
/ O O\__ NOT |
/ \ feed the |
/ \ \ trolls |
/ _ \ \ ______________|
/ |\____\ \ ||
/ | | | |\____/ ||
/ \|_|_|/ \ __||
/ / \ |____| ||
/ | | /| | --|
| | |// |____ --|
* _ | |_|_|_| | \-/
*-- _--\ _ \ // |
/ _ \\ _ // | /
* / \_ /- | - | |
* ___ c_c_c_C/ \C_c_c_c_______ _____

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Jan 3 '06 #32
Kenny McCormack wrote
(in article <dp**********@y in.interaccess. com>):
In article <dp**********@n wrdmz02.dmz.ncs .ea.ibs-infra.bt.com>,
Richard Heathfield <in*****@invali d.invalid> wrote:
(The usual RH psychobabble - deleted)
(Then clayne says)
But hey, it's that important to be right, right?
It can be, yes, when lives are at stake. Badly-written code can kill people.


You need to learn how to distinguish between Usenet and real life.


And also to recognize when they intersect.
When you can show me where the contents of a Usenet post affect life/death
in the real world (i.e., not in the funny little online world), then we'll
talk, OK?


Simple: Post the source code to a virus here. The real world
will stand up and take notice.

More obscure way: Fail to teach students properly, they go to
Usenet to get help, they get bad advice, then graduate anyway,
go on to employment in a nuclear power plant, on missile
guidance systems, space shuttle management software, etc.
People die.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Jan 3 '06 #33
nelu wrote:
Companies like Siemens use C in a lot of programs used in cars and
airplanes, some of them in vital places. There are a lot of cases of
tragedies (cars and planes) related to badly written code (even C).


Hate to break it to you, but they aren't coming to comp.lang.c for code
auditing. Neither are the R+D crew of nuclear reactors and air traffic
control management software.

Jan 4 '06 #34

clayne wrote:
Hate to break it to you, but they aren't coming to comp.lang.c for code
auditing. Neither are the R+D crew of nuclear reactors and air traffic
control management software.

Oh, I forgot that those people are born with special mutations that
make them C gurus before they get out of the womb.
Maybe rocket scientists don't come here, but there's a chance that some
of them were here before becoming rocket scientists... and maybe
someone who will become a rocket scientist is already here, trying to
learn and getting incomplete information because they were not directed
to the specialists.
You probably know a lot about rockets to help with an answer, but, if
you are wrong, how many people in here are going to correct you?
And errors propagate exponentially.. . even if they can't. :-)

Jan 4 '06 #35
Keith Thompson wrote:
_______________ ____
/| /| | |
||__|| | Please do |
/ O O\__ NOT |
/ \ feed the |
/ \ \ trolls |
/ _ \ \ ______________|


Self-important guy, it's not trolling. We're bringing up valid issues
with valid content and dispute. Just because you fail to agree with or
recognize observations outside of your own
little world does not mean they can be automatically written off as
"trolls trolling." But I understand - it's intellectually easier for
you to cast off that which you don't agree with. All part of that
asperger's thing again, I guess.

Jan 4 '06 #36
Markus Becker wrote:
They have a
problem, ask and discuss, sometimes even learning something
during the discussions and be glad afterwards that they
were able to solve their problems. Perhaps they even have
learned something that helps them to avoid problems or
errors of similar categories in the future?

Markus, score adjusted


Because typically the answers are snide, strident, and self-important -
with a subtle goal of the regulars trying to out-correct/identify
mistakes of others. There's typically not much pragmatism exercised and
answers are full of implied perfectionism.

Jan 4 '06 #37
In article <11************ ********@g14g20 00cwa.googlegro ups.com>,
clayne <cl****@anodize d.com> wrote:
Because typically the answers are snide, strident, and self-important -
with a subtle goal of the regulars trying to out-correct/identify
mistakes of others. There's typically not much pragmatism exercised and
answers are full of implied perfectionism.


Well, this -is- comp.LANG.c . It might be different if it were
comp.PROGRAMMIN G.c .
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Jan 4 '06 #38
Walter Roberson wrote:
Because typically the answers are snide, strident, and self-important -
with a subtle goal of the regulars trying to out-correct/identify
mistakes of others. There's typically not much pragmatism exercised and
answers are full of implied perfectionism.


Well, this -is- comp.LANG.c . It might be different if it were
comp.PROGRAMMIN G.c .
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes


And what verse of the bible quotes: "Thou shalt have right to be snide
asshole if posting to comp.lang.* but thou shalt exercise the restraint
of the holy one if stricken by the filth of comp.programmin g.*" ? I
think that's in Ecclesiastes somewhere as well.

Jan 4 '06 #39
clayne wrote:
But I understand - it's intellectually easier for you to cast off
that which you don't agree with. All part of that asperger's thing
again, I guess.


Asparagus?
Jan 4 '06 #40

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2328
by: Tony Proctor | last post by:
Hi everyone We're experiencing some serious anomalies with the scheduling of ASP threads. I'd be interested to hear if anyone knows what algorithm is used (e.g. simple round-robin, or something more sophisticated), and what situations might perturb it. Even a hint as to what would be considered normal scheduling might help. The root of our problem is that we observed a normally well-behaved web application suddenly limit itself to a...
7
2706
by: Ivan | last post by:
Hi I have following problem: I'm creating two threads who are performing some tasks. When one thread finished I would like to restart her again (e.g. new job). Following example demonstrates that. Problem is that when program is started many threads are created (see output section), when only two should be running at any time. Can you please help me to identify me where the problem is? Best regards
20
3036
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
6
23746
by: Tomaz Koritnik | last post by:
I have a class that runs one of it's method in another thread. I use Thread object to do this and inside ThreadMethod I have an infinite loop: While (true) { // do something Thread.Sleep(100); } The problem is that I don't know how to terminate the thread when my class
13
5101
by: Bob Day | last post by:
Using vs2003, vb.net I start a thread, giving it a name before start. Code snippet: 'give each thread a unique name (for later identification) Trunk_Thread.Name = "Trunk_0_Thread" ' allow only 1 thread per line Trunk_Thread.ApartmentState = ApartmentState.STA
4
2565
by: fred | last post by:
I use a Synclock in a secondary thread and also stop the thread using the abort method. If the abort occurs while the thread is in the Synclock will the SyncLock always be released before the thread stops or do I need to add some extra code to avoid Synclock staying on after the thread has been stopped. Thanks Fred
51
54887
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() # This thread is expected to finish within a second
14
6892
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a datagridview on the form. I am using the following code to spawn the worker thread... Thread WorkerThread = new Thread(new ThreadStart(WT_MyFunction)); WorkerThread.IsBackground = true; WorkerThread.Start(); The problem I am having is...I cannot seem...
7
5901
by: Sin Jeong-hun | last post by:
Hi. I'm writing a Client/Multi-threaded Server program on Windows Vista. It worked fine on Windows Vista, but when the server ran on Windows XP, I/O operation has been aborted because of either a thread exit or an application request exception randomly occurred at the OnReceive method (asynchronous tcp stream reading). I searched all over the internet and found a post posted few years ago. He had the same problem as me, and he said it
34
2827
by: Creativ | last post by:
Why does Thread class not support IDisposable? It's creating quite some problem. Namely, it can exhaust the resource and you have not control over it.
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9565
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.