-1

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?

jxh
  • 69,070
  • 8
  • 110
  • 193
  • There is no secret here. You have two easy ways of determining what the thread does: One, the `void *(*)(void *)`-argument determines which function the thread starts in, Two, the `void*` determines what the argument to that function will be. – EOF Apr 07 '16 at 23:36
  • Every threads automatically goes to PartialCodec function and the argument is same for all the threads – Rohith Reddy Apr 07 '16 at 23:41
  • 1
    You are not showing all of your code, but we are assuming `iTd` is different for each thread, which should mean each thread is getting a different pointer to an element of `pcMCData`. – jxh Apr 07 '16 at 23:43
  • Why would you do that if you want the threads to do different tasks? – EOF Apr 07 '16 at 23:43
  • the final code in this website is basically what I want to do. http://www.thegeekstuff.com/2012/04/create-threads-in-linux/ but in my case the variable `tid` is not global and so i cant use it. I need a workaround for this – Rohith Reddy Apr 07 '16 at 23:49
  • *Why* do you need to do something like that? Why don't you just pass an appropriate, uniquely identifying argument as the `void*`? – EOF Apr 07 '16 at 23:54
  • This is actually part of my project and I have been provided with a set of code which i shouldn't change. I can only write code inside the `PartialCodec` function. – Rohith Reddy Apr 07 '16 at 23:59
  • 1
    @RohithReddy: Well, if your codebase comes pre-broken, you obviously won't be able to use an elegant solution. You can always \*shudder\* use a global variable protected by a mutex to pass additional information between the threads if sane means of communication have been sabotaged. – EOF Apr 08 '16 at 00:11
  • What @EOF says. If your task come with ridiculous restrictions applied by your prof, why are you wasting time asking us? Ask your prof how to do it since s/he is the one causing the problem. – Martin James Apr 08 '16 at 00:16
  • The only sane apporach is that suggested by @immibis below - make all threads capable of doing all jobs and provide a task ID var in the passed control struct, (maybe an enum), that the thread code can switch on to do the commanded task. – Martin James Apr 08 '16 at 00:19
  • @MartinJames yeah, I will talk to my professor. – Rohith Reddy Apr 08 '16 at 01:29

2 Answers2

1

Assuming you know how many threads will be calling PartialCodec, you can use static variables within the function to facilitate communication between threads. A bare static would allow all threads in PartialCodec to manipulate the same object instance.

void * PartialCodec (void *arg) {
    static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
    static struct state {
        /* some state */
    } critical;
    struct state local;

    pthread_mutex_lock(&lock);
    local = critical; /* make a local copy */
    /* update critical */
    pthread_mutex_unlock(&lock); 

    /* ... refer to local copy of state ... */
}

The critical state tracks which part of the problem a particular thread should solve. Copying it to local establishes that the thread will work on that part of the problem. The critical state is then updated so the next thread that reads critical will know to work on a different part of the problem.

jxh
  • 69,070
  • 8
  • 110
  • 193
0

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]);
user253751
  • 57,427
  • 7
  • 48
  • 90