I’m making a c file for a dispatch queue that gets a task and put it in to a queue which is the linked list. In order to do this, I need to create threads using
pthread_t cThread;
if(pthread_create(&cThread, NULL, work, param)){
perror("ERROR creating thread.");
}
However I need to make another function that goes into ‘work’ and ‘param’ variable as parameters of create function. My friend told me that I just need to put any code in the work function that loops infinitely so the thread does not die.. Can anyone explain each parameter goes in to the pthread_create function- especially for work and param? I searched Google for this, but most of tutorials are so hard to understand the concept…
The four parameters to
pthread_createare, in order:A pointer to a
pthread_tstructure, whichpthread_createwill fill out with information on the thread it creates.A pointer to a
pthread_attr_twith parameters for the thread. You can safely just passNULLmost of the time.A function to run in the thread. The function must return
void *and take avoid *argument, which you may use however you see fit. (For instance, if you’re starting multiple threads with the same function, you can use this parameter to distinguish them.)The
void *that you want to start up the thread with. PassNULLif you don’t need it.