gpt4 book ai didi

c - pthread 和 ncurses - 为什么这个锁不起作用?

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:59 25 4
gpt4 key购买 nike

我正在学习 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com