- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在学习 c 和 ncurses,并使用 halfdelay(1) 编写了一个简单的贪吃蛇游戏来实现半自动化。当我添加线程时它停止工作,因为我无法从键盘获得任何输入。
所以我写了一个小程序来尝试线程/ncurses 和互斥锁。该代码有 2 个线程,每个线程都有自己的函数(fn 和 fn2)。当我在相应的函数调用中使用 wmove/wprint 运行它时,它可以工作(好吧,它打印到屏幕上!)我尝试实现互斥锁定并将代码移动到另一个名为 print_to_screen() 的函数,但没有任何反应。
我添加了 if(thread_id == pthread_self())
以在线程之间切换,但仍然没有任何反应。我添加了几行注释掉的行,它们显示 2 个线程到达 print_to_screen() 函数,但 if 循环中的代码根本没有运行。
请帮助我,我已经束手无策了——我什至尝试阅读 usr/inc.ncurses.h
,但人却一点帮助也没有!这是代码。如果您注释掉 2 个函数中的 wmove/wprint 位并取消注释 print_to_screen() 调用,您可以看到不同之处。 ncurses 设置来自 invisible-island 教程,如果有人知道他为什么将 (void) 放在他的 ncurses 调用前面,请告诉我。 TIA,感谢您提供的任何帮助。
#include <stdlib.h>
#include <curses.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
void *myfunction(void *);
void *myfunction2(void *);
void print_to_screen(void);
static void finish(int);
pthread_t thread1, thread2;
pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
int thread1_id, thread2_id, count, count2;
int done;
int ch;
WINDOW *mywin;
int main(void){
(void) signal(SIGINT, finish); /* arrange interrupts to terminate */
(void) initscr(); /* initialize the curses library */
keypad(stdscr, TRUE); /* enable keyboard mapping */
(void) nonl(); /* tell curses not to do NL->CR/NL on output */
(void) cbreak(); /* take input chars one at a time, no wait for \n */
(void) echo(); /* echo input - in color */
if (has_colors())
{
start_color();
/*
* Simple color assignment, often all we need. Color pair 0 cannot
* be redefined. This example uses the same value for the color
* pair as for the foreground color, though of course that is not
* necessary:
*/
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_CYAN, COLOR_BLACK);
init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
}
count = count2 = 0;
done = 0;
mywin = newwin(LINES, COLS, 0,0);
keypad(mywin, TRUE);
pthread_create( &thread1, NULL, myfunction, NULL);
pthread_create( &thread2, NULL, myfunction2, NULL);
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
finish(0);
exit(0);
}
void *myfunction(void *ptr){
thread1_id = pthread_self();
halfdelay(-1);
while(1){
ch = getch();
if(ch == 'q') break;
wmove(mywin, 5,2);
wprintw(mywin, "Thread 1 at print");
wmove(mywin, 10,count);
waddch(mywin, ch);
wrefresh(mywin);
//print_to_screen();
if(count++ >70) count = 0;
nanosleep((struct timespec[]){{0, 250000000}}, NULL);
}
done = 1;
}
void *myfunction2(void *ptr){
thread2_id = pthread_self();
while(1){
if(done == 1) break;
if(count++ >24) count = 0;
wmove(mywin, 6,2);
wprintw(mywin, "Thread 2 at print");
wmove(mywin, count, 10);
wprintw(mywin, "hello from thread 2");
wrefresh(mywin);
//print_to_screen();
nanosleep((struct timespec[]){{0, 250000000}}, NULL);
}
}
void print_to_screen(){
pthread_mutex_lock(&mx);
//printw("PTHREAD ID = %d : ", pthread_self());
//printw("thread1_id = %d : thread2_id = %d\n", thread1_id, thread2_id);
if(pthread_self() == thread1_id){
wmove(mywin, 5,2);
wprintw(mywin, "Thread 1 at print");
wmove(mywin, 10,count);
waddch(mywin, ch);
wrefresh(mywin);
}
if(pthread_self() == thread2_id){
wmove(mywin, 6,2);
wprintw(mywin, "Thread 2 at print");
wmove(mywin, count, 10);
wprintw(mywin, "hello from thread 2");
wrefresh(mywin);
}
pthread_mutex_unlock(&mx);
}
static void finish(int sig)
{
endwin();
/* do your non-curses wrapup here */
}
最佳答案
作为开始:
int thread1_id, thread2_id, count, count2;
应该是
int count, count2;
pthread_t thread1_id, thread2_id;
print_to_screen()
也从未被调用。
关于c - pthread 和 ncurses - 为什么这个锁不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22423580/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!