2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HSPでマルチプロセス/共有メモリ

Posted at

素のHSPでマルチスレッドはほぼできないのでexecでマルチプロセスを組み込む。
今回はこちらをお借りして共有メモリで値の取り交わしを行ってみた。
近いうちに面倒臭い部分すべて隠蔽してさらにモジュール化できたら面白そうだけど難しいかなぁ。

# packopt name "MPN"
# runtime "hsp3cl"
# include "MemoryMapModule.as"

*Entry
	//exeファイル位置
	#uselib "kernel32"
	#func GetModuleFileName "GetModuleFileNameA" int,int,int
	sdim fullExePath, 256
	GetModuleFileName,varptr(fullExePath),256
	ExeName=getpath(fullExePath,8)
	deli=", "	//区切り文字
	
	cmd=dir_cmdline
	//サブプロセスの判別
	if 0<=instr(cmd,,"$ProcesS$") {
		//関数の取得
		split cmd,"$PFuncID$",nul,PFuncID,paramStr
		ProcessFuncLoad
		//関数をコールバックで投げ込んで処理を任せる
		subProcess List@ProcessFunc(int(PFuncID))
	}
	else {
		main
	}
end

//プロセスに投げ込む関数
# module ProcessFunc
	#deffunc ProcessFuncLoad
		//ラベルリスト
		List=*delayWait
	return
	//gosubゲッター(引数)
	#define global params(%1=nul,%2=nul,%3=nul,%4=nul,%5=nul) split paramStr@,"$ParamS$",%1,%2,%3,%4,%5
	//gosubセッター(戻り値)
	#deffunc resolve var value
		returnValue@HSProcess=value
	return

*delayWait:params _wait,_PID
		await int(_wait)*10
		resolve _wait
	return
# global

//サブプロセスの処理用モジュール
# module HSProcess
	//共有メモリ作成定義
	count=0
	#define global memInt(%1,%2="") newmod PGate,MemoryMapModule,ExeName@+%2,256: Mmry_DupMemory PGate(count),%1:count++
	#define global memStr(%1,%2="") newmod PGate,MemoryMapModule,ExeName@+%2,256: Mmry_DupMemory PGate(count),%1,2:count++
	//一括プロセス起動
	#define global ProcessRuns(%1,%2) foreach %1: \
		exec ExeName@+" "+"$ProcesS$$PFuncID$"+%2+"$PFuncID$"+int(%1(cnt))+"$ParamS$"+cnt: \
	loop
	//プロセス待ち合わせ
	#define global ProcessAll(%1) repeat: if length(%1)=PCnt {break}: loop
	//String.Join
	#defcfunc joinStr array _array,str _deli
		_str=""
		foreach _array
			if cnt!=0:_str+=_deli
			_str+=_array(cnt)
		loop
	return _str

	//サブプロセス
	#deffunc subProcess var ProcessCall
		memInt PCnt,"PCnt"
		memStr PIDs,"PIDs"
		memStr PResult,"PResult"

		gosub ProcessCall
		Result=returnValue
		params ,PID
	
		if 0<PCnt {
			PResult=PResult+deli@
			PIDs=PIDs+deli@
		}
		PResult=PResult+str(Result)
		PIDs=PIDs+str(PID)
		PCnt++
	return
# global

//メインプロセス
# module Program
	#uselib "msvcrt"
	#func printf "printf" str

	#deffunc main
		printf("【MultiProcessExec】\n> ")
		//input _input,256,1
		_input="500 300 100 15 72 10"
		mes _input
		split _input," ",items
		memInt PCnt,"PCnt"
		memStr PIDs,"PIDs"
		memStr PResult,"PResult"

		ProcessRuns items,0
		ProcessAll items

		split PResult,deli@,sortResult
		mes "done!"
		mes joinStr(sortResult,deli@)

		input exit,,1
	return
# global
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?