2012-04-15 40 views
1

我對pthread和xlib編程非常新穎。我試圖繪製和移動兩組相反方向的矩形,但是當我運行我的代碼時,我的問題只有一個線程被執行,另一個被忽略。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); 
    } 
    } 
+1

一次只有一個線程可以做X11調用 – 2012-04-15 07:21:19

+0

@BasileStarynkevitch所以你的意思是我想要做什麼將是不可能的?或者你有什麼建議? – fanbondi 2012-04-15 07:30:19

+0

的確,我建議只有一個線程在執行Xlib調用。使用像GTK或QT這樣的工具包,只有主線程才能執行GUI調用。如果你絕對想要有多個線程在執行X11(這對我來說是錯誤的),請使用互斥量進行序列化,並且不要忘記經常調用Xsync。 – 2012-04-15 07:33:21

回答

0

你有循環和usleep()電話內的互斥鎖的,這意味着第一個執行的線程將永遠鎖定另一個線程。

+0

@geekosuar這是我嘗試過的最後一個。即使沒有互斥體,它也保持不變..謝謝。 – fanbondi 2012-04-15 07:27:31

+1

如果沒有互斥鎖,你很幸運不會得到核心轉儲,因爲我沒有在任何地方看到'XInitThreads()'。 Xlib確實不是線程安全的;你最好不要嘗試使用線程。 – geekosaur 2012-04-15 07:32:03

1

Xlib支持來自多個線程的調用只有在調用任何其他Xlib函數之前調用XInitThreads()之前纔有效。即使如此,仍然存在一些bug,但是如果沒有它,就不會嘗試在線程之間進行適當的鎖定或同步。

2

不要運行,這是你對閃爍的燈光很敏感。

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()會導致不斷的閃爍。在一個真正的程序中,每個線程將負責清除舊框架的部分,而不觸及其他部分。