11e9 Subj : Re: passing an 'int' to a thread To : comp.programming.threads From : doug Date : Wed Jun 08 2005 05:43 pm "doug" wrote in message news:t7ype.16027$bl3.8901@fe1.news.blueyonder.co.uk... > > Damn, I forgot the DGCs! > > "doug" wrote in message > news:n2ype.238581$Cq2.10303@fe2.news.blueyonder.co.uk... >> Look for DGC below. >> >> "Huub" wrote in message >> news:42a671a3$0$156$3a628fcd@reader2.nntp.hccnet.nl... >>> You were right. The errors were due to a linking problem. And I made a >>> another mistake which I corrected. Now something is being passed, but >>> it's not the correct value. Could you tell me what is being passed: >>> task_ids or t? >>> >>> New code: >>> >>> >>> #ifdef HAVE_CONFIG_H >>> #include >>> #endif >>> >>> #include >>> #include >>> #include >>> #include >>> #include >>> #include "ftapi/ftapi.h" >>> >>> using namespace std; >>> using namespace ftapi; >>> >>> // default interface connection >>> string device(""); >>> ft_if_conn connection = FT_IC_LIB_USB; >>> IfOutputs out; >>> IfInputs inp; >>> >>> void *MotorDraad(void *threadid) >>> { >>> int *id_ptr, task_id; >>> id_ptr = (int *)threadid; >>> task_id = *id_ptr; >>> cout << "Start MotorDraad" << endl; >>> time_t start; >>> time_t current; >>> time(&start); >>> try >>> { >>> auto_ptr fti (ftGetIfFromFactory(device, >>> connection)); >>> // Initialspeed = task_id >>> FtMotorSpeed mSpeed1(task_id); >>> cout << "Snelheid1 = " << task_id << endl; >>> // Initialspeed = task_id >>> FtMotorSpeed mSpeed2(task_id); >>> cout << "Snelheid2 = " << task_id << endl; >>> // enable all motor outputs >>> out.data[FT_MASTER]= 0xFF; >>> // M1: not moving >>> out.setMotorSpeed(FT_MASTER, FT_M1, mSpeed1); >>> // M2: forward with velocity 4 >>> out.setMotorSpeed(FT_MASTER, FT_M2, mSpeed2); >>> while (difftime(current,start) < 5.0) >>> { >>> time(¤t); >>> fti->writeAndReadAllData(out, inp); >>> } >>> } >>> // anything went wrong >>> catch (exception &x) >>> { >>> cout << x.what() << endl; >>> } >>> } >>> >>> >>> int main(int argc, char *argv[]) >>> { >>> cout << "Start fase 1" << endl; >>> >>> pthread_t threads[1]; >>> int *task_ids[1]; >>> int rc, t; >>> t = 0; >>> int snelheid = 0; >>> >>> cout << "1 Thread gedeclareerd" << endl; >>> >>> cout << "fttestextension v0.4" << endl; >>> >>> if (1 == argc) >>> { >>> /* leave device name as defined above, since no >>> parameters given */ >>> } >>> else if (((2 == argc) || (3 == argc)) && >>> (string("-l")==(argv[1]))) >>> { >>> if (3 == argc) device = argv[2]; >>> connection = FT_IC_LIB_USB; >>> } >>> else if (((2 == argc) || (3 == argc)) && >>> (string("-u")==(argv[1]))) >>> { >>> if (3 == argc) >>> { >>> device = argv[2]; >>> } >>> else >>> { >>> device = "/dev/usb/robointerface0"; >>> } >>> connection = FT_IC_USB; >>> } >>> else >>> { >>> return EXIT_FAILURE; >>> } >>> >>> // now let's go! >>> cout << "opening fischertechnik interface \"" << device << "\"" >>> << endl; >>> task_ids[t] = (int *) malloc(sizeof(int)); >> > > DGC > >> You should be malloc'ing sizeof(int*) here. IGNORE me here. First thing in the morning, and all. >> >>> *task_ids[t] = t; >>> while (snelheid != 8) >>> { >>> cin >> snelheid; >>> cout << "Aanmaken thread" << endl; >>> cout << "t = " << t << endl; >>> t = snelheid; // new piece of code >> > > DGC > >> Here you assign a new value to t... >> >>> rc = pthread_create(&threads[t], NULL, MotorDraad, >>> (void *)task_ids[t]); >>> //what is passed: task_ids or t? >> > > DGC > >> Tha value passed is task_ids[t], which is an int pointer. However, just >> above you assigned a new value to t, so you likely haven't set up >> task_ids[t]. So you're probably passing an uninitialised, unless you can >> guarantee that t is always zero. (In which case, why are you reading it >> from cin [via snelheid]?) >> >>> } >>> pthread_exit(NULL); >>> return EXIT_SUCCESS; >>> } >>> >>> // EOF >>> >>> Thank you for helping out. >> >> Hope that helps. Not sure, though. >> > > > . 0