Raised This Month: $12 Target: $400
 3% 

Creating threads


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Miu
Veteran Member
Join Date: Nov 2013
Old 03-22-2015 , 19:48   Creating threads
Reply With Quote #1

I'm trying to make a sourcemod extension to create a thread to execute any given plugin function with the simple code:

Code:
DWORD WINAPI thr(LPVOID param)
{
	((IPluginFunction *)param)->CallFunction(0, 0, 0);
	return 0;
}

cell_t CreateThreadNative(IPluginContext *pContext, const cell_t *params)
{
	IPluginFunction *pfn = pContext->GetFunctionById(params[1]);
	CreateThread(0, 0, thr, pfn, 0, 0);
	return 0;
}
Testing this on arbitrary functions in a plugin seemingly works fine on some functions, gives the unclear "Plugin encountered error 17: Stack memory leaked by native" other times, and completely crashes the server other times again. Sourcemod can also no longer unload the plugin.

I'm not sure why the code I wrote causes this erratic behavior. Any help?
Miu is offline
KyleS
SourceMod Plugin Approver
Join Date: Jul 2009
Location: Segmentation Fault.
Old 03-22-2015 , 20:41   Re: Creating threads
Reply With Quote #2

https://mxr.alliedmods.net/sourcemod...Threader.h#414

The execution environment isn't threadsafe.
KyleS is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 03-23-2015 , 14:10   Re: Creating threads
Reply With Quote #3

ipluginfunction::callfunction() isn't threadsafe?
Miu is offline
VoiDeD
AlliedModders Donor
Join Date: Mar 2009
Location: Illinois, USA
Old 03-23-2015 , 15:31   Re: Creating threads
Reply With Quote #4

The SP runtime is only ever expected to operate on the game's main thread.

You should not be calling into or out of SP from another thread unless you're serializing access between your threads, which can defeat the purpose of having a separate thread in the first place.

If you're doing some expensive operations that you want to move to another thread, a generic producer/consumer pattern is used where you can hook GameFrame to see if any of your queued jobs are completed/failed/etc.
__________________

Last edited by VoiDeD; 03-23-2015 at 15:31.
VoiDeD is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 03-23-2015 , 20:22   Re: Creating threads
Reply With Quote #5

ok, guess it's not possible, thx~
Miu is offline
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

Forum Jump


All times are GMT -4. The time now is 06:27.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode