clinuxmultithreadingpthreadslinuxthreads

Differentiate between threads called by pthread


A supplied framework is calling my function PartialCodec.

pthread_create(&(pcPthread[iTd]),NULL,PartialCodec,(void *)&pcMCData[iTd]);

I am not allowed to change the framework code. However, inside PartialCodec I want to assign different tasks for different threads and so I need to differentiate between each thread. How can I do this?


Solution

  • Use the argument, Luke.

    You're passing &pcMCData[iTd] as the thread argument.

    So just add some fields to that structure, telling the thread which tasks to work on. (And obviously set those fields before creating the thread)

    pcMCData[iTd].thingToWorkOn = /* however you decide the thing to work on */;
    pthread_create(&(pcPthread[iTd],NULL,PartialCodec,(void *)&pcMCData[iTd]);