Local POSIX stream data in C

I am trying to give each thread some data related to the stream; can this be done using the stream parameter?

So, when creating a stream, I pass the variable, and in the stream function do I change its value and use it as specific data for each stream?

int main(void){
    ...
    int Tparam = 0;
    ...
    rc = pthread_create(&threads[c1], NULL, Thread_Pool, (void *)Tparam);
    ...
}

then in the Thread_Pool function I use it like this:

void *Thread_Pool(void *param){
    int id;
    id = (int) param;
    id = value; // can this value be a specific value for this thread?
}
+3
source share
4 answers

This can help if you are shown as advertised Tparam.

However, if you want each stream to have its own space for storing some data, you can organize the transfer of this space to the stream as an argument to the function. For instance:

enum { NTHREADS = 10 };

struct TLS_Data
{
    int   id;
    char  buffer[2048];
    size_t index;
} data[NTHREADS];

for (int c1 = 0; c < NTHREADS; c1++)
{
    data[c1].index = c1;
    data[c1].id = 0;
    data[c1].buffer[0] = '\0';
    int rc = pthread_create(&threads[c1], NULL, Thread_Pool, &data[c1]);
    ...handle errors, etc...
}

pthread_create(); void *, .

Thread_Pool , ; . , ; , :

uintptr_t value = c1 + 10;

rc = pthread_create(&threads[c1], NULL, Thread_Pool, (void *)value);

value uintptr_t, , , . , .

, , , , , (Thread_Pool() ), , . , , , :

uintptr_t value = c1 + 10;

rc = pthread_create(&threads[c1], NULL, Thread_Pool, &value);

( ), , . !

+4

?

, -, , , :

int id = *((int*) param);

. . void, void .

- ? , .

- - Linux. . . , , . , , .

. , , , . - , , .

, , , . , :

void threadfunc(void* param)
{
    int id = /* ??? */
}

int id, , . .

, :

int tParam[] = {1,2,3};

rc = pthread_create(&threads[1], NULL, Thread_Pool, (void *)&(tParam[1]));
rc = pthread_create(&threads[2], NULL, Thread_Pool, (void *)&(tParam[2]));
rc = pthread_create(&threads[3], NULL, Thread_Pool, (void *)&(tParam[3]));

..

+2

pthread_self(). . . API URL- LLNL, juampa.

+1

All Articles