我是pthread和xlib编程的新手。我试图在相反的方向上绘制和移动两组矩形,但我的问题是,当我运行代码时,只执行其中一个线程,而忽略另一个线程。
我试过几种方法,但都没有用。任何建议都将不胜感激。
我已经包含了我的源代码。
谢谢。
编译: gcc -o Demofinal Demofinal.c -lpthread -lX11
运行:./Demofinal
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <pthread.h>
//Compile gcc -o Demofinal Demofinal.c -lpthread -lX11
//Run ./Demofinal
Display *dis;
Window win;
XEvent report;
GC green_gc;
XColor green_col;
Colormap colormap;
pthread_mutex_t mutexsum;
char green[] = "#00FF00";
void *createMoveLogs( void *ptr );
typedef struct str_thdata
{
int xval;
int yval;
int dir;
} thdata;
int main() {
pthread_t mvleft, mvright;
thdata data1, data2;
/* initialize data to pass to thread 1 and 2*/
data1.xval = 600;
data1.yval = 115;
data1.dir = 0;
data2.xval = 5;
data2.yval = 250;
data2.dir = 1;
dis = XOpenDisplay(NULL);
win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 1, 1, 550, 550, 0, WhitePixel (dis, 0), WhitePixel(dis, 0));
XMapWindow(dis, win);
colormap = DefaultColormap(dis, 0);
green_gc = XCreateGC(dis, win, 0, 0);
XParseColor(dis, colormap, green, &green_col);
XAllocColor(dis, colormap, &green_col);
XSetForeground(dis, green_gc, green_col.pixel);
pthread_create( &mvleft, NULL, createMoveLogs, (void*) &data1);//thread 1 move right to left
pthread_create( &mvright, NULL, createMoveLogs, (void*) &data2);//thread 2 move left to right
pthread_join(mvleft, NULL);
pthread_join(mvright, NULL);
return 0;
}
void *createMoveLogs( void *ptr )
{
thdata *data;
data = (thdata *) ptr;
int x= data->xval; int y = data->yval; int i;
int direction = data->dir;
//check if the direction to move == left
if(direction == 0){
pthread_mutex_lock(&mutexsum);
for(i=0; i<=600; i++){
XDrawRectangle(dis, win, green_gc, x, y, 145, 50);
XDrawRectangle(dis, win, green_gc, x+200, y, 145, 50);
XDrawRectangle(dis, win, green_gc, x+400, y, 145, 50);
x-=10;
if (x==-500){x=data->xval; i=0;}
usleep(100000);
XFlush(dis);
XClearWindow(dis, win);
}
pthread_mutex_unlock(&mutexsum);
}else if(direction == 1){
pthread_mutex_lock(&mutexsum);
for(i=0; i<=600; i++){
XDrawRectangle(dis, win, green_gc, x, y, 145, 50);
XDrawRectangle(dis, win, green_gc, x-200, y, 145, 50);
XDrawRectangle(dis, win, green_gc, x-400, y, 145, 50);
x+=10;
if (x==855){x=5; i=0;}
usleep(100000);
XFlush(dis);
XClearWindow(dis, win);
}
pthread_mutex_lock(&mutexsum);
}
}发布于 2013-06-19 05:10:44
不要跑这是因为你对闪光很敏感。
XInitThreads();
...
if(direction == 0){
for(i=0; i<=600; i++){
pthread_mutex_lock(&mutexsum);
printf ("Thread %d\n", direction);
XDrawRectangle(dis, win, green_gc, x, y, 145, 50);
XDrawRectangle(dis, win, green_gc, x+200, y, 145, 50);
XDrawRectangle(dis, win, green_gc, x+400, y, 145, 50);
x-=10;
if (x==-500){x=data->xval; i=0;}
XFlush(dis);
XClearWindow(dis, win);
usleep(50000);
pthread_mutex_unlock(&mutexsum);
usleep(50000);
}
}else if(direction == 1){
for(i=0; i<=600; i++){
pthread_mutex_lock(&mutexsum);
printf ("Thread %d\n", direction);
XDrawRectangle(dis, win, green_gc, x, y, 145, 50);
XDrawRectangle(dis, win, green_gc, x-200, y, 145, 50);
XDrawRectangle(dis, win, green_gc, x-400, y, 145, 50);
x+=10;
if (x==855){x=5; i=0;}
XFlush(dis);
XClearWindow(dis, win);
usleep(50000);
pthread_mutex_unlock(&mutexsum);
usleep(50000);
}
}这将显示两组移动的矩形。当然,这种方法完全是原始的,因为XClearWindow()会导致持续的闪烁。在真正的程序中,每个线程负责清除自己的旧框架部分,而不接触其他部分。
发布于 2013-06-18 10:38:18
仅当在调用任何其他Xlib函数之前调用XInitThreads()时,Xlib才支持来自多个线程的调用。即使这样,仍然有一些bug,但如果没有它,就不会尝试在线程之间进行适当的锁定或同步。
发布于 2012-04-15 15:25:34
在互斥锁中有循环和usleep()调用,这意味着要执行的第一个线程将永远锁定另一个线程。
https://stackoverflow.com/questions/10160245
复制相似问题