thinking Posted July 17, 2006 Share Posted July 17, 2006 hiho@lli was interested in creating threads in php (don't know for what, just for fun now ;-) )what i want:[code]<?php function testfunc(){ print("IT WORKS"); } create_thread("testfunc");?>[/code]explanation: the above code, creates a thread, the thread runs and prints "IT WORKS" (1)the same stuff should work with objects and some parameters would be cool, to give the thread function (2)the example (1) works, that's not my problem nowmy problem is (2)using objects, and parametersthe code i have:[CODE]/* {{{ PHP_MCLS_CREATESOCKET */PHP_FUNCTION(create_thread){ zval *functionname; if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"z",&functionname)==FAILURE){ RETURN_FALSE; } switch(Z_TYPE_P(functionname)){ case IS_STRING:{ pthread_t mclsth; pthread_create(&mclsth,NULL,thread_func,functionname->value.str.val); pthread_detach(mclsth); RETURN_TRUE; } default: RETURN_FALSE; } RETURN_TRUE;}/* }}} */void* thread_func(void *arg){ char *funcname = (char*)arg; zval *func_name; zval *return_value; if(funcname == NULL){ zend_error(E_ERROR,"MCLS: Function is null"); } MAKE_STD_ZVAL(func_name); ZVAL_STRING(func_name, "testfunc", 1); ZEND_SET_SYMBOL(EG(active_symbol_table), "func_name", func_name); TSRMLS_FETCH(); if(call_user_function_ex(CG(function_table), NULL, func_name, &return_value, 0, NULL, 0,&EG(symbol_table)) != FAILURE){ return NULL; } zend_error(E_ERROR,"Couldn't call function %s",funcname); return NULL;}[/CODE]this all works with my extensionbut since i'm a noob in programming extensions i have no ideahow can i differentiate between create_thread("myfunc"); OR create_thread(array($object,"myfunc")); and how can i make "forward" this to thread_func? i think i will need something like a wrapper structure, which holds my function call data and parameters but since i'm a zendnoob i have no idea, how the php memory management works and that's why i'm not sure how it worksto be more concrete: Q1: what is missing for my C code to accept object methods or functions? (example code would be great)Q2: what is missing for my C code to accept parameters for the threadfunction?Q3: when, how and why do i have to free some zval vars?[edit]i forgot to mention that i'm running php-5.1.4[/edit]thx@ll Quote Link to comment Share on other sites More sharing options...
ASDen Posted August 20, 2007 Share Posted August 20, 2007 For the Threading matter read this[url=http://www.phpfreaks.com/forums/index.php?topic=155604.0]http://www.phpfreaks.com/forums/index.php?topic=155604.0[/url] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.