void *writeloop(void *arg) {}

What is the point here passing argument as a pointer to pointer function?

 
  void *writeloop(void *arg) {}
need more input...
do you have a full code sample?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

int g_ant = 0;         /* global declaration */

void *writeloop(void *arg) {
 while (g_ant < 10) {
  g_ant++;
  usleep(rand()%10);
  printf("%d\n", g_ant);
 }
 exit(0);
}

int main(void)
{
 pthread_t tid;
 pthread_create(&tid, NULL, writeloop, NULL);
 writeloop(NULL);
 pthread_join(tid, NULL);
 return 0;
}

pthread is code from before c++ got its threading tools.
If I remember this right, arg is simply a way for the threading library to pass arguments to the thread function, in case it needs some data. Here, its all dummy info, so its just to fill in the blanks that the library needs, but that is where I am lost too. From memory, it was ok to have a thread function with no argument, and the pthread call was either a different overload or you put in nullptr or something and it went unused. I just don't remember, but its likely either required to make the library calls work, or, the coder thought that and did not know the way to do it without the dummy parameter.

edit... I did a quick lookup to see and it looks like the dummy is required for pthreads to work. So its just a placeholder to make it work, because that is what the library requires.
Last edited on
Registered users can post here. Sign in or register to post.