当前位置: 技术问答>linux和unix
关于linux多线程的一个问题!
来源: 互联网 发布时间:2016-03-26
本文导语: 代码如下,为什么这样,不能实现两个视频同时播放,把在our_thread1和our_thread2中的gdk_threads_enter();gdk_threads_leave();两个去掉,两个视频就可以同时播放,这是为什么? #include static GtkWidget *fixed; static GtkWidget *but...
代码如下,为什么这样,不能实现两个视频同时播放,把在our_thread1和our_thread2中的gdk_threads_enter();gdk_threads_leave();两个去掉,两个视频就可以同时播放,这是为什么?
#include
static GtkWidget *fixed;
static GtkWidget *button1;
static GtkWidget *button2;
void our_thread1(char* t1)
{
gdk_threads_enter();
system(t1);
gdk_threads_leave();
}
void our_thread2(char* t2)
{
gdk_threads_enter();
system(t2);
gdk_threads_leave();
}
void on_begin()
{
g_thread_create(our_thread1,"mplayer -geometry 70:80 1.avi",FALSE,NULL);
g_thread_create(our_thread2,"mplayer -geometry 70:80 2.avi",FALSE,NULL);
}
int main(int argc,char* argv[])
{
GtkWidget *window,*view;
if (!g_thread_supported())
g_thread_init(NULL);
gdk_threads_init();
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"thread apllication");
g_signal_connect(G_OBJECT(window),"delete_event",
G_CALLBACK(gtk_main_quit),NULL);
gtk_container_set_border_width(GTK_CONTAINER(window),10);
button=gtk_button_new_with_label("Start");
gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,5);
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(on_begin),NULL);
gtk_widget_show_all(window);
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
return FALSE;
}
#include
static GtkWidget *fixed;
static GtkWidget *button1;
static GtkWidget *button2;
void our_thread1(char* t1)
{
gdk_threads_enter();
system(t1);
gdk_threads_leave();
}
void our_thread2(char* t2)
{
gdk_threads_enter();
system(t2);
gdk_threads_leave();
}
void on_begin()
{
g_thread_create(our_thread1,"mplayer -geometry 70:80 1.avi",FALSE,NULL);
g_thread_create(our_thread2,"mplayer -geometry 70:80 2.avi",FALSE,NULL);
}
int main(int argc,char* argv[])
{
GtkWidget *window,*view;
if (!g_thread_supported())
g_thread_init(NULL);
gdk_threads_init();
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"thread apllication");
g_signal_connect(G_OBJECT(window),"delete_event",
G_CALLBACK(gtk_main_quit),NULL);
gtk_container_set_border_width(GTK_CONTAINER(window),10);
button=gtk_button_new_with_label("Start");
gtk_box_pack_start(GTK_BOX(vbox),button,FALSE,FALSE,5);
g_signal_connect(G_OBJECT(button),"clicked",
G_CALLBACK(on_begin),NULL);
gtk_widget_show_all(window);
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
return FALSE;
}
|
gdk_threads_enter就是互斥锁,一个占有了,另一个就得等待,当然只能有一个播放了。