AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Coding MM:S Plugins & SM Extensions (https://forums.alliedmods.net/forumdisplay.php?f=75)
-   -   Creating threads (https://forums.alliedmods.net/showthread.php?t=260291)

Miu 03-22-2015 19:48

Creating threads
 
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?

KyleS 03-22-2015 20:41

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

The execution environment isn't threadsafe.

Miu 03-23-2015 14:10

Re: Creating threads
 
ipluginfunction::callfunction() isn't threadsafe?

VoiDeD 03-23-2015 15:31

Re: Creating threads
 
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.

Miu 03-23-2015 20:22

Re: Creating threads
 
ok, guess it's not possible, thx~


All times are GMT -4. The time now is 01:25.

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