c passing several arguments to threads

前端 未结 4 428
暗喜
暗喜 2021-01-14 21:21

when i create a thread, i want to pass several arguments. So i define in a header file the following:

struct data{
  char *palabra;
  char *directorio;
  FI         


        
4条回答
  •  难免孤独
    2021-01-14 21:39

    Do not use option #2. The data structure could be overwritten (explicitly, for instance using the same structure to start another thread, or implicitly, for instance having it overwritten on the stack). Use option #1.

    To get at your data, at the start of your thread, do

    struct data *info = (struct data*)arguments;
    

    Then access info as normal. Make sure to free it when the thread is done (or, as I prefer, have the caller free it after joining with the thread).

提交回复
热议问题