SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 25/06/2011, 06:15 AM   #1
Fro1sha
Big Clucker
 
Fro1sha's Avatar
 
Join Date: Nov 2009
Posts: 168
Reputation: 22
Default MultiThread

Description:

This is MultiThread plugin. And I don't know what to write here . Examples speak for themselves.
http://en.wikipedia.org/wiki/Multith...rchitecture%29
Attention:
Quote:
PAWN is not designed to be multi-threaded and there are no guarantees AT ALL on your data the moment you introduce threads.
Wrong using this plugin can significantly corrupt your gamemode.
How to use:
pawn Code:
#include <thread>

Log:
  • 0.1.6

    - Added new native CreateThreadEx( const pubname[ ] );
  • 0.1.5

    - First release.

Natives:
pawn Code:
native CreateThread( const pubname[ ] );
native CreateThreadEx( const pubname[ ] );
native DestroyThread( threadid );
native SleepThread( milliseconds ); // Use in thread (public)
native LockThread( threadid ); // Synchronize will return the id lock
native UnLockThread( lockid );

Example:
With CreateThread
pawn Code:
new tickid;

public OnGameModeInit()
{
    tickid = CreateThread( "MyFunction" );
}

public OnGameModeExit()
{
    DestroyThread( tickid );
}

public MyFunction( threadid )
{
    printf( "tick(%d)...", threadid  );
       
    SleepThread( 1000 );
}
With CreateThreadEx
pawn Code:
new bool:active;
new tickid;

public OnGameModeInit()
{
    tickid = CreateThreadEx( "MyFunction" );
}

public OnGameModeExit()
{
    active = false;
}

public MyFunction( threadid )
{
    active = true;
   
    while ( active )
    {
        printf( "tick(%d)...", threadid  );
       
        SleepThread( 1000 );
    }
}

Download:
__________________
[Plugin] MultiThread
[Plugin] GMT Timezone
[Plugin] Regular expression

Development any plugins, for the money.
.

Last edited by Fro1sha; 26/06/2011 at 06:43 AM.
Fro1sha is offline   Reply With Quote
Old 25/06/2011, 06:50 AM   #2
linuxthefish
High-roller
 
linuxthefish's Avatar
 
Join Date: Dec 2010
Posts: 2,989
Reputation: 681
Default Re: MultiThread

I'm confused about what this dose, is it just a different way to call a function?
linuxthefish is offline   Reply With Quote
Old 25/06/2011, 06:59 AM   #3
Fro1sha
Big Clucker
 
Fro1sha's Avatar
 
Join Date: Nov 2009
Posts: 168
Reputation: 22
Default Re: MultiThread

Quote:
Originally Posted by linuxthefish View Post
I'm confused about what this dose, is it just a different way to call a function?
http://en.wikipedia.org/wiki/Multith...rchitecture%29

If you speak Russian, i can explain it to you =)))
__________________
[Plugin] MultiThread
[Plugin] GMT Timezone
[Plugin] Regular expression

Development any plugins, for the money.
.
Fro1sha is offline   Reply With Quote
Old 25/06/2011, 07:02 AM   #4
linuxthefish
High-roller
 
linuxthefish's Avatar
 
Join Date: Dec 2010
Posts: 2,989
Reputation: 681
Default Re: MultiThread

Quote:
Originally Posted by Fro1sha View Post
http://en.wikipedia.org/wiki/Multith...rchitecture%29

If you speak Russian, i can explain it to you =)))
I didn't think it was to do with actual threads! I'm definitely going to be using this.
linuxthefish is offline   Reply With Quote
Old 25/06/2011, 07:35 AM   #5
Ronaldo_raul
High-roller
 
Ronaldo_raul's Avatar
 
Join Date: Jun 2010
Location: Gujarat, India.
Posts: 1,105
Reputation: 67
Default Re: MultiThread

will get in handy for ....pro scripter.....not a beginner like me.....
Ronaldo_raul is offline   Reply With Quote
Old 25/06/2011, 07:45 AM   #6
Rysoku
Big Clucker
 
Join Date: Jun 2011
Location: SC4MPT0N
Posts: 69
Reputation: 3
Default Re: MultiThread

Can't this corrupt your computer?
__________________
The best comedy

Rysoku is offline   Reply With Quote
Old 25/06/2011, 07:51 AM   #7
Fro1sha
Big Clucker
 
Fro1sha's Avatar
 
Join Date: Nov 2009
Posts: 168
Reputation: 22
Default Re: MultiThread

Quote:
Originally Posted by ronaldoraul View Post
will get in handy for ....pro scripter.....not a beginner like me.....
No this is very simple.

Quote:
Originally Posted by Rysoku View Post
Can't this corrupt your computer?
What?
__________________
[Plugin] MultiThread
[Plugin] GMT Timezone
[Plugin] Regular expression

Development any plugins, for the money.
.
Fro1sha is offline   Reply With Quote
Old 25/06/2011, 07:58 AM   #8
langricr
Big Clucker
 
langricr's Avatar
 
Join Date: Jun 2011
Location: Bowmanville, Ontario
Posts: 73
Reputation: 1
Default Re: MultiThread

I was actually wondering why there didn't seem to be a threading plugin yet, based on the source, the parameter known as 'pubname' defines a function to be executed by the thread. Also, I'm guessing the function loops until the thread is destroyed? If so, you should change that so that threads can die naturally without being told to, it is much easier to implement a loop than to perform logic to assume when would be a good time to call DestroyThread. That way you could also implement a OnThreadDeath callback.

That way something like this would work.
Code:
new bool:active;
new tickid;

public OnGameModeInit()
{
	tickid = ThreadCreate( "MyFunction" );
}

public OnGameModeExit()
{
	active = false;
}

public MyFunction()
{
	active = true;
	
	while ( active )
	{
		printf( "tick..." );
		
		SleepThread( 1000 );
	}
}

public onThreadDeath( threadid )
{
	if ( threadid = tickid )
		printf( "The tick thread has died naturally." );
}
Other than that, nice work!
langricr is offline   Reply With Quote
Old 25/06/2011, 08:18 AM   #9
Fro1sha
Big Clucker
 
Fro1sha's Avatar
 
Join Date: Nov 2009
Posts: 168
Reputation: 22
Default Re: MultiThread

Quote:
Originally Posted by langricr View Post
I was actually wondering why there didn't seem to be a threading plugin yet, based on the source, the parameter known as 'pubname' defines a function to be executed by the thread. Also, I'm guessing the function loops until the thread is destroyed? If so, you should change that so that threads can die naturally without being told to, it is much easier to implement a loop than to perform logic to assume when would be a good time to call DestroyThread. That way you could also implement a OnThreadDeath callback.

That way something like this would work.
Code:
new bool:active;
new tickid;

public OnGameModeInit()
{
	tickid = ThreadCreate( "MyFunction" );
}

public OnGameModeExit()
{
	active = false;
}

public MyFunction()
{
	active = true;
	
	while ( active )
	{
		printf( "tick..." );
		
		SleepThread( 1000 );
	}
}

public onThreadDeath( threadid )
{
	if ( threadid = tickid )
		printf( "The tick thread has died naturally." );
}
Other than that, nice work!
This is variant not good. Very hard for pawn scripters and DestroyThread and not actual onThreadDeath( threadid ).
__________________
[Plugin] MultiThread
[Plugin] GMT Timezone
[Plugin] Regular expression

Development any plugins, for the money.
.
Fro1sha is offline   Reply With Quote
Old 25/06/2011, 08:23 AM   #10
langricr
Big Clucker
 
langricr's Avatar
 
Join Date: Jun 2011
Location: Bowmanville, Ontario
Posts: 73
Reputation: 1
Default Re: MultiThread

Quote:
Originally Posted by Fro1sha View Post
This is variant not good. Very hard for pawn scripters and DestroyThread and not actual onThreadDeath( threadid ).
I suppose people could just call DestroyThread at the end of the function, but it seems dangerous outside of that, what happens if you try to destroy a thread while its halfway through the function?

I recommend having a look at this, while its Java, it does give some insight as to the dangers of stopping a thread.

Quote:
Stopping a thread with Thread.stop causes it to unlock all of the monitors that it has locked (as a natural consequence of the unchecked ThreadDeath exception propagating up the stack). If any of the objects previously protected by these monitors were in an inconsistent state, the damaged objects become visible to other threads, potentially resulting in arbitrary behavior. Many uses of stop should be replaced by code that simply modifies some variable to indicate that the target thread should stop running.
__________________
WinSock Plugin - Create socket connections using your server for network I/O


langricr is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MultiThread in Streamer Fro1sha Screenshots and Videos 0 03/06/2011 05:25 AM


All times are GMT. The time now is 03:49 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.