线程问题...... [英] thread problem...

查看:69
本文介绍了线程问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线程的使用方面遇到了问题,他们不能按我的意愿工作

to。

这是代码,问题是,如果我取消注释

// pthread_join(thread_ID,NULL);,主程序将停止,直到

生成的线程完成,但那是'不是预期的方式,因为我需要opc变量只有当用户按下一个键时才会改变,我认为它与pthread_attr_set(属性)有关

选项,但我没有得到答案的人(3)。

我需要它的方式是主要继续在while内循环

即使线程还没有完成。

顺便说一句,我没有做mygetch功能,但这不是问题

的主题。


// ---------------------------- --------------------------------

//这个例子试图显示使用pthreadss,

//它只在Linux上运行,b由于pthread.h

//头文件。

//

//它有很多bug,你只能运行它一个终端

//在一个X-session中启动,否则会崩溃,主要问题是
//问题是我注释了pthread_join行,并且

//我做了那个,因为该行等待线程

//结束,在这个例子中它不会工作,我几乎是

//确保更改attr属性可以使这个

//运行得更好,无论如何,我没有找到解决方案,另一个

/ /问题是线程运行得太快,所以它很难很好地预测角色的移动。

//所以,如果你认为你可以解决这些问题,请

//给我发邮件,或者只是修改代码,但是,请

//知道,''因为我真的想要学习解决方案。

//

//作者:Juan Francisco Benavides Nanni。

//邮件: el*****@gmail.com

//网址: http://mmabj.tk

// date :2005年12月29日

// ---------------------------------- --------------------------


#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

#定义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();

开关(opc){

案例UP:

if(y> MIN)y - = 1;

其他y = MAXWIDY;

car =''^'';

休息;

case DOWN:

if(y< MAXWIDY)y + = 1;

else y = MIN;

car = ''v'';

休息;

案例LEFT:

if(x> MIN)x - = 1;

其他x = MAXWIDX;

car =''<'';

休息;

案例RIG:

if(x< MAXWIDX)x + = 1;

else x = MIN;

car =''>'';

休息;

案例销售:

休息;

默认:

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);

返回ch;

}

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: el*****@gmail.com
// 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;
}

推荐答案

2005年12月31日20:56:03 -0800,el ***** @ gmail.com < el ***** @ gmail.com>

在comp.lang.c中写道:
On 31 Dec 2005 20:56:03 -0800, "el*****@gmail.com" <el*****@gmail.com>
wrote in comp.lang.c:
我在使用中遇到问题线程,他们不工作,因为我想要他们



这里偏离主题,C语言没有定义或支持线程。

线程是该语言的非标准扩展,并且是

在不同平台上的实现方式不同。


[snip]

// ------------- -----------------------------------------------
//此示例尝试显示pthreadss的使用,
//它仅在Linux上运行,因为pthread.h
//头文件。
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]


请求Linux线程帮助的地方是

news:comp.os.linux.development.apps 。祝你好运。


-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c < a rel =nofollowhref =http://c-faq.com/target =_ blank> 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



[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


正确的位置是在comp.programming.threads中询问。


杰克,< pthread.h>是指POSIX线程,而不是Linux线程。

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

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


" el ***** @ gmail.com" < EL ***** @ gmail.com>写道:
"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.




标准C不支持线程。试试comp.programming.threads。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



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

--
Keith Thompson (The_Other_Keith) 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.


这篇关于线程问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆