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.