You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hi and thanks for this great job
I have a structure that im allocating memory for each thread and finally each thread has their own data structure for executing function
but I don't know how to pass their own thread_data while the task is in queue and I don't know which thread will execute it
struct thread_data
{
/* some stuff */
};
void func(thread_data* data, int x)
{
/* some stuff */
}
int main(int argc, char** argv)
{
int num_thread = 4;
thread_data* data = (thread_data*)malloc(num_thread * sizeof(thread_data)); // struct array for each thread
if (data == NULL) return 1;
thread_pool pool(num_thread);
for (auto i = 1; i <= 100; i++)
pool.push_task(func, &data[0], i); // !HERE! &data[0], &data[1], ... ?
return 0;
}
if two or more threads are using the same structure, program crashes, they should have their own unique structure. How to solve this problem?
The text was updated successfully, but these errors were encountered:
Immortal-guard
changed the title
passing thread data structure to thread pool
passing thread data structure
Aug 26, 2021
Sounds like you're having issues with data races. See a discussion here under "Threads and data races". There are several standard ways to handle this, such as using std::atomic or std::mutex. Both are used in the thread pool's code, so you are welcome to check it out as an example.
hi and thanks for this great job
I have a structure that im allocating memory for each thread and finally each thread has their own data structure for executing function
but I don't know how to pass their own thread_data while the task is in queue and I don't know which thread will execute it
if two or more threads are using the same structure, program crashes, they should have their own unique structure. How to solve this problem?
The text was updated successfully, but these errors were encountered: