■スレッドを使ったプログラムのコンパイルについて
○スレッド
■ボタンを押すとスレッドを作成して、スレッドからボタンの文字を変更します
ボタンを複数回押すと複数のスレッドが作成されて、複数のスレッドからボタンの文字が変更されます
複数から描画&フラッシュされるとメモリリークしますので排他制御したほうが良いかもしれません
 #include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
Widget button1;
//ボタンやラベルの文字を変更する
void setCaption(Widget w,char*str){
	Arg args[1];
	XtSetArg(args[0],XtNlabel,(XtArgVal)str);
	XtSetValues(w,args,1);
	XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True);//再描画
	XFlush(XtDisplay(w));//バッファをフラッシュ
}
//ボタンやラベルの文字を取得する
void getCaption(Widget w,char*str){
	char*st;
	Arg args[1];
	XtSetArg(args[0],XtNlabel,(XtArgVal)&st);
	XtGetValues(w,args,1);
	strcpy(str,st);
}
//スレッドメソッド
void *thread(void *arg){
	int i;
	char s[128];
	for(i=0;i<10;i++){
		sleep(1);
		sprintf(s,"%d",i);
		setCaption(button1,s);
	}
}
//ボタンが押された時の動作
void Button1Callback( Widget w, XtPointer client, XtPointer called )
{
	//スレッドを作成
	pthread_t thread1;
	int i=pthread_create(&thread1,NULL,thread,(void *)"Thread1");
	if(i) printf("err\n");
}
main( int argc, char **argv )
{
	XtAppContext app_context;
	Widget top;
	top = XtVaAppInitialize( &app_context, "test", NULL, 0, &argc, argv, NULL,NULL);
	Widget form = XtVaCreateManagedWidget("form",formWidgetClass,top,NULL);
	//ボタンの作成
	button1 = XtVaCreateManagedWidget("button1",//ウィジット名
					commandWidgetClass,//ボタンクラスを指定
					form,
					//リソースの直接入力(どんどん追加できる)
					XtNlabel,"button1",//表示される文字
					XtNwidth,100,//幅と高さ
					XtNheight,20,
					XtNborderWidth,1,//境界線
					//リソースここまで
					NULL);
	XtAddCallback(button1, XtNcallback,Button1Callback,(XtPointer)NULL);//ボタンのコールバック関数を登録
	XtRealizeWidget( top );
	XtAppMainLoop( app_context );
}
■排他制御を付けたプログラム
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
Widget button1;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
//ボタンやラベルの文字を変更する
void setCaption(Widget w,char*str){
	pthread_mutex_lock(&m);//ロックする
	Arg args[1];
	XtSetArg(args[0],XtNlabel,(XtArgVal)str);
	XtSetValues(w,args,1);
	XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True);//再描画
	XFlush(XtDisplay(w));//バッファをフラッシュ
	pthread_mutex_unlock(&m);//ロックを解除する
}
//ボタンやラベルの文字を取得する
void getCaption(Widget w,char*str){
	char*st;
	Arg args[1];
	XtSetArg(args[0],XtNlabel,(XtArgVal)&st);
	XtGetValues(w,args,1);
	strcpy(str,st);
}
//スレッドメソッド
void *thread(void *arg){
	int i;
	char s[128];
	for(i=0;i<10;i++){
		sleep(1);
		sprintf(s,"%d",i);
		setCaption(button1,s);
	}
}
//ボタンが押された時の動作
void Button1Callback( Widget w, XtPointer client, XtPointer called )
{
	//スレッドを作成
	pthread_t thread1;
	int i=pthread_create(&thread1,NULL,thread,(void *)"Thread1");
	if(i) printf("err\n");
}
main( int argc, char **argv )
{
	XtAppContext app_context;
	Widget top;
	top = XtVaAppInitialize( &app_context, "test", NULL, 0, &argc, argv, NULL,NULL);
	Widget form = XtVaCreateManagedWidget("form",formWidgetClass,top,NULL);
	//ボタンの作成
	button1 = XtVaCreateManagedWidget("button1",//ウィジット名
					commandWidgetClass,//ボタンクラスを指定
					form,
					//リソースの直接入力(どんどん追加できる)
					XtNlabel,"button1",//表示される文字
					XtNwidth,100,//幅と高さ
					XtNheight,20,
					XtNborderWidth,1,//境界線
					//リソースここまで
					NULL);
	XtAddCallback(button1, XtNcallback,Button1Callback,(XtPointer)NULL);//ボタンのコールバック関数を登録
	XtRealizeWidget( top );
	XtAppMainLoop( app_context );
}
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
Widget button1;
//ボタンやラベルの文字を変更する
void setCaption(Widget w,char*str){
	Arg args[1];
	XtSetArg(args[0],XtNlabel,(XtArgVal)str);
	XtSetValues(w,args,1);
	XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True);//再描画
	XFlush(XtDisplay(w));//バッファをフラッシュ
}
//ボタンやラベルの文字を取得する
void getCaption(Widget w,char*str){
	char*st;
	Arg args[1];
	XtSetArg(args[0],XtNlabel,(XtArgVal)&st);
	XtGetValues(w,args,1);
	strcpy(str,st);
}
//スレッドメソッド
void *thread(void *arg){
	int i;
	char s[128];
	for(i=0;i<10;i++){
		sleep(1);
		sprintf(s,"%d",i);
		setCaption(button1,s);
	}
}
//ボタンが押された時の動作
void Button1Callback( Widget w, XtPointer client, XtPointer called )
{
	//スレッドを作成
	pthread_t thread1;
	int i=pthread_create(&thread1,NULL,thread,(void *)"Thread1");
	if(i) printf("err\n");
}
main( int argc, char **argv )
{
	XtAppContext app_context;
	Widget top;
	top = XtVaAppInitialize( &app_context, "test", NULL, 0, &argc, argv, NULL,NULL);
	Widget form = XtVaCreateManagedWidget("form",formWidgetClass,top,NULL);
	//ボタンの作成
	button1 = XtVaCreateManagedWidget("button1",//ウィジット名
					commandWidgetClass,//ボタンクラスを指定
					form,
					//リソースの直接入力(どんどん追加できる)
					XtNlabel,"button1",//表示される文字
					XtNwidth,100,//幅と高さ
					XtNheight,20,
					XtNborderWidth,1,//境界線
					//リソースここまで
					NULL);
	XtAddCallback(button1, XtNcallback,Button1Callback,(XtPointer)NULL);//ボタンのコールバック関数を登録
	XtRealizeWidget( top );
	XtAppMainLoop( app_context );
}
■排他制御を付けたプログラム
#include <stdio.h>
#include <sys/types.h>
#include <pthread.h>
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Command.h>
Widget button1;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
//ボタンやラベルの文字を変更する
void setCaption(Widget w,char*str){
	pthread_mutex_lock(&m);//ロックする
	Arg args[1];
	XtSetArg(args[0],XtNlabel,(XtArgVal)str);
	XtSetValues(w,args,1);
	XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True);//再描画
	XFlush(XtDisplay(w));//バッファをフラッシュ
	pthread_mutex_unlock(&m);//ロックを解除する
}
//ボタンやラベルの文字を取得する
void getCaption(Widget w,char*str){
	char*st;
	Arg args[1];
	XtSetArg(args[0],XtNlabel,(XtArgVal)&st);
	XtGetValues(w,args,1);
	strcpy(str,st);
}
//スレッドメソッド
void *thread(void *arg){
	int i;
	char s[128];
	for(i=0;i<10;i++){
		sleep(1);
		sprintf(s,"%d",i);
		setCaption(button1,s);
	}
}
//ボタンが押された時の動作
void Button1Callback( Widget w, XtPointer client, XtPointer called )
{
	//スレッドを作成
	pthread_t thread1;
	int i=pthread_create(&thread1,NULL,thread,(void *)"Thread1");
	if(i) printf("err\n");
}
main( int argc, char **argv )
{
	XtAppContext app_context;
	Widget top;
	top = XtVaAppInitialize( &app_context, "test", NULL, 0, &argc, argv, NULL,NULL);
	Widget form = XtVaCreateManagedWidget("form",formWidgetClass,top,NULL);
	//ボタンの作成
	button1 = XtVaCreateManagedWidget("button1",//ウィジット名
					commandWidgetClass,//ボタンクラスを指定
					form,
					//リソースの直接入力(どんどん追加できる)
					XtNlabel,"button1",//表示される文字
					XtNwidth,100,//幅と高さ
					XtNheight,20,
					XtNborderWidth,1,//境界線
					//リソースここまで
					NULL);
	XtAddCallback(button1, XtNcallback,Button1Callback,(XtPointer)NULL);//ボタンのコールバック関数を登録
	XtRealizeWidget( top );
	XtAppMainLoop( app_context );
}
▲トップページ
 > 
Linux と C