Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AbrahamR, 1 invisible), 858 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
[FREE]Multithreading Plugin #313289
02/28/10 16:46
02/28/10 16:46
Joined: May 2009
Posts: 445
Peine, Germany
Razoron Offline OP
Senior Member
Razoron  Offline OP
Senior Member

Joined: May 2009
Posts: 445
Peine, Germany
Hey guys,
since I bought my new PC with a Quadcore, I wondered why gamestudio don't use all 4 processors. I googled it and saw, that just some programs support "Multithreading" to use all processors.
In the future, there will be just "Multithreading" programs and games, I think.
So, i coded a little plugin for you to create threads for your games.

UPDATES:
v0.2:
-Added fuctions "CreateAThreadWithVar" and "CreateAThreadWithInt" to overgive parameters to your thread function.
-You can create unlimited Threads.

You can download it here.
Scroll down until you see "Multithreading Plugin" and click on it.

There are 2 examples:
fileexample.c
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>

///////////////////////////////

void CreateAThread(void*func);
void MultithreadingInit();

var fhandle;

void f1()
{	
	while(1)
	{		
		file_var_write(fhandle,1);
		wait(-0.5);
	}
}

void f2()
{	
	while(1)
	{		
		file_var_write(fhandle,2);
		wait(-0.5);
	}
}

void f3()
{	
	while(1)
	{		
		file_var_write(fhandle,3);
		wait(-0.5);
	}
}

void f4()
{	
	while(1)
	{		
		file_var_write(fhandle,4);
		wait(-0.5);
	}
}

void main()
{
	fhandle=file_open_write("test.txt");
	MultithreadingInit();
	CreateAThread(f1);
	CreateAThread(f2);
	CreateAThread(f3);
	CreateAThread(f4);	
}


This example creates/opens test.txt. With "CreateAThread" you create a thread, that is running a function, so there are 4 Threads. The function you want to use may not have any parameters.
Code:
void f1()
{	
	while(1)
	{		
		file_var_write(fhandle,1);
		wait(-0.5);
	}
}

void f2()
{	
	while(1)
	{		
		file_var_write(fhandle,2);
		wait(-0.5);
	}
}

....


Thread 1 writes "1" in test.txt and waits 0.5 seconds. Thread 2 writes 2 and so on....
Run it about 5 seconds, then take a look at test.txt.
It should look like this:
Quote:

1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2 1 4 3 2

Now the next example, crashtest.c.
Code:
i=0;
while(1)
{
     i++;
}


will crash, because you don't use wait(1).
Open crashtest.c.
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>

///////////////////////////////

void CreateAThread(void*func);
void MultithreadingInit();

var fhandle;

STRING*str="#100";

TEXT*txt=
{
	string=str;
	pos_x=100;
	pos_y=100;
	flags=SHOW;
}

void f1()
{
	var i=0;
	while(1)
	{
		i++;
		i--;
	}
}

void f2()
{	
	var i=0;
	while(1)
	{
		i++;
		i--;
	}
}

void f3()
{	
	var i=0;
	while(1)
	{
		i++;
		i--;
	}
}

void f4()
{
	var i=0;	
	while(1)
	{
		str_for_num(str,i);
		i++;
		wait(1);	
	}
}

void main()
{
	MultithreadingInit();
	CreateAThread(f1);
	CreateAThread(f2);
	CreateAThread(f3);
	CreateAThread(f4);	
}


It looks a bit like fileexample.c.
But you know functions like
Code:
void f1()
{
	var i=0;
	while(1)
	{
		i++;
		i--;
	}
}


would crash, because there is no wait(1).
Yes, thread 1, thread 2 and thread 3 are useless. They are just utilizing 100% processing power of their thread.
Thread 4,
Code:
STRING*str="#100";

TEXT*txt=
{
	string=str;
	pos_x=100;
	pos_y=100;
	flags=SHOW;
}
....
void f4()
{
	var i=0;	
	while(1)
	{
		str_for_num(str,i);
		i++;
		wait(1);	
	}
}


is copying every frame of thread 4 var i into a string, which is shown in a TEXT. Thread 4 is working without problems.
I hope you can use this in you project. Feedbacks, problems and so on are welcome.

Regards,
Henning

EDIT1:Release 4 free!

Last edited by Razoron; 06/06/10 19:04.
Re: Multithreading Plugin [Re: Razoron] #313313
02/28/10 18:00
02/28/10 18:00
Joined: May 2005
Posts: 2,713
Lübeck
Slin Offline
Expert
Slin  Offline
Expert

Joined: May 2005
Posts: 2,713
Lübeck
Quote:
At the moment, you are not allowed to use it in any commercial project.

Happily, there is no need for such a plugin, as you can just call the functions your plugin uses directly from Lite-C wink

Re: Multithreading Plugin [Re: Slin] #313323
02/28/10 18:33
02/28/10 18:33
Joined: May 2009
Posts: 445
Peine, Germany
Razoron Offline OP
Senior Member
Razoron  Offline OP
Senior Member

Joined: May 2009
Posts: 445
Peine, Germany
grin. Yes, you are right.
Code:
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>


and some know-how how to use CreateThread are enaugh, but the plugin is for the ones, who don't want to know how and to make it easy. grin
I will release it 4 free.

Last edited by Razoron; 02/28/10 18:38.
Re: Multithreading Plugin [Re: Razoron] #313329
02/28/10 19:08
02/28/10 19:08
Joined: Oct 2007
Posts: 5,210
Ä°stanbul, Turkey
Quad Offline
Senior Expert
Quad  Offline
Senior Expert

Joined: Oct 2007
Posts: 5,210
Ä°stanbul, Turkey
last time i remember, engine functions that auto allocates space for structs(ent_create,pan_create etc) in sperate threads caused crashes.(some sort of thing like engine losing sync or something like that.)

could be useful for mathematical calculations.

I can be completely wrong though.


3333333333
Re: Multithreading Plugin [Re: Quad] #313332
02/28/10 19:23
02/28/10 19:23
Joined: May 2009
Posts: 445
Peine, Germany
Razoron Offline OP
Senior Member
Razoron  Offline OP
Senior Member

Joined: May 2009
Posts: 445
Peine, Germany
Yes, multithreading for 3dgs is very sensitive. In fileexample.c, i tried wait(-0.1) indstead of wait(-0.5). Now the engine writes chiniese grin.

EDIT:pan_create works great 4 me.

Last edited by Razoron; 02/28/10 19:29.
Re: Multithreading Plugin [Re: Razoron] #313448
03/01/10 16:02
03/01/10 16:02
Joined: Jan 2004
Posts: 3,023
The Netherlands
Helghast Offline
Expert
Helghast  Offline
Expert

Joined: Jan 2004
Posts: 3,023
The Netherlands
What is multithreading exactly? I heard of it, never looked into it...

regards,


Formerly known as dennis_fantasy
Portfolio - http://www.designorhea.com/
Project - http://randomchance.cherrygames.org/
Re: Multithreading Plugin [Re: Helghast] #313459
03/01/10 16:19
03/01/10 16:19
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
running two programs on different systems or parts of the program asynchronised and still be able to communicate with eachother. If you make a call to a database on a webserver, it may take a while before you get a response. You don't always want to have your game frozen up and wait for response, so if you write the database-communication part in a separate thread, that thread will wait for answer while your game can continue running.


Click and join the 3dgs irc community!
Room: #3dgs
Re: Multithreading Plugin [Re: Joozey] #314225
03/06/10 19:41
03/06/10 19:41
Joined: May 2009
Posts: 445
Peine, Germany
Razoron Offline OP
Senior Member
Razoron  Offline OP
Senior Member

Joined: May 2009
Posts: 445
Peine, Germany
*Bump for update*


Moderated by  aztec, Blink, HeelX 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1