473,789 Members | 2,833 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 #1
39 1879
On 31 Dec 2005 20:56:03 -0800, "el*****@gmail. com" <el*****@gmail. com>
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.li nux.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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 1 '06 #2
The correct place would be to ask in comp.programmin g.threads.

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

Jan 1 '06 #3
"el*****@gmail. com" <el*****@gmail. com> 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.programmin g.threads.

--
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 1 '06 #4
"clayne" <cl****@anodize d.com> writes:
The correct place would be to ask in comp.programmin g.threads.

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


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

--
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 1 '06 #5
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" <cl****@anodize d.com> writes:
The correct place would be to ask in comp.programmin g.threads.

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


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


Jan 1 '06 #6
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)
Jan 1 '06 #7
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.

Jan 1 '06 #8
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.
Jan 1 '06 #9
In article <11************ **********@g44g 2000cwa.googleg roups.com>,
clayne <cl****@anodize d.com> 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...

Jan 1 '06 #10

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
2705
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
3035
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
23745
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
5097
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
2561
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
54881
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
6890
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
2818
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
9666
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
9511
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
10200
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...
0
9984
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...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2909
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.