pthread-join

pthread_join error code 3

独自空忆成欢 提交于 2020-01-07 02:58:10
问题 i have problem in my project. it throws me error code 3. I just add part of my code to let you see what I did. in main.cpp I declared on the threads then I send to initRequestThreads(in thread.h) to create the threads. then in main.cpp i let the main process to wait for it. main.cpp pthread_t *requestersThreads = new pthread_t[Length_Tasks]; requestsPool->initRequestThreads(&requestersThreads); void* status; // wait for all requests threads for(t=0; t<Length_Tasks; t++) { rc = pthread_join

passing for loop index into pthread_create argument object in C

随声附和 提交于 2019-12-29 09:57:30
问题 I would like to pass my for loop's index into the pthread_create's argument through a wrapper object. However, the printed integer from the thread is incorrect. I expected the below code to print out, in no particular order. id is 0, id is 1, id is 2, id is 3, However it prints this instead and integer 1,3 is never passed into a thread id is 0, id is 0, id is 0, id is 2, struct thread_arg { int id; void * a; void * b; } void *run(void *arg) { struct thread_arg * input = arg; int id = input-

pthread_join() for asynchronous threads

陌路散爱 提交于 2019-12-23 14:57:04
问题 I have written a simple demonstration program so that I can understand the pthread_join() function. I know how to use the pthread_condition_wait() function to allow asynchronous threading but I'm trying to understand how I can do similar work using the pthread_join() function. In the program below I pass Thread 1s ID to Thread 2s function. Inside Thread 2s function I call the pthread_join() function and pass in Thread 1s ID. I expected this to cause Thread 1 to run first and then for Thread 2

Confused about the argument in pthread_create()

女生的网名这么多〃 提交于 2019-12-23 05:10:29
问题 My question :why not just pass &i as the last argument to pthread_create()? instead he create a array to hold the same thing.... #define THREAD_CT 2 /* bump this up a few numbers if you like */ void *print_stuff(void *ptr) { int i, id= * (int *) ptr; for (i= 0; i < 5; i++) { printf("Thread %d, loop %d.\n", id, i); sleep(rand() % 2); /* sleep 0 or 1 seconds */ } printf("Thread %d exiting.\n", id); return NULL; } int main(void) { pthread_t tids[THREAD_CT]; int i, ids[THREAD_CT]; for (i= 0; i <

Peterson's Algorithm to avoid race condition between threads

僤鯓⒐⒋嵵緔 提交于 2019-12-13 01:34:40
问题 Details: I am implementing Peterson's Algorithm(below) to avoid race condition. The way I want to do it, is to declare a global integer variable, and create threads one and two. Whenever the thread one had access to the global variable it should print a and add one to the global variable counter. When the thread two have access to this global variable it should print b and add one to the global variable counter. This should continue until the global variable reaches a certain number(let's say

pthread: join a detached thread doesn't set errno correctly

邮差的信 提交于 2019-12-12 14:26:22
问题 I am checking the behavior of 'pthread_join' and have the following code: #include <stdio.h> #include <stdlib.h> #include <assert.h> #include <errno.h> #include <pthread.h> void *thread(void *vargp) { pthread_detach(pthread_self()); pthread_exit((void *)42); } int main() { int i = 1; pthread_t tid; pthread_create(&tid, NULL, thread, NULL); sleep(1); pthread_join(tid, (void **)&i); printf("%d\n", i); printf("%d\n", errno); } Observed output on my platform (Linux 3.2.0-32-generic #51-Ubuntu SMP

Memory leakage in windows pthread. `pthread_join` does not deallocate memory

蹲街弑〆低调 提交于 2019-12-12 03:29:20
问题 The simple test: void testMemoryLeak_PthreadCreateJoin(void) { auto taskFunction = [](void*args) -> void* { return nullptr; }; pthread_t pth; int err = pthread_create(&pth, /*attr*/nullptr, taskFunction, /*args*/nullptr); pthread_join(pth, nullptr); } void main(void) { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); testMemoryLeak_PthreadCreateJoin(); testMemoryLeak_PthreadCreateJoin(); testMemoryLeak_PthreadCreateJoin(); testMemoryLeak_PthreadCreateJoin(); } Here said: A thread

pthread_exit issues returning struct

╄→尐↘猪︶ㄣ 提交于 2019-12-11 19:27:24
问题 I have a struct typedef struct something_t { int a; int b; } VALUES; In my thread function I do VALUES values; values.a = 10; values.b = 11; pthread_exit((void*)&values); And I try to receive by doing VALUES values; pthread_join(thread, (void*)&values); printf("A: %d\B: %d\n", values.a, values.b); The values I receive are weird every time. I am confused about how to receive the values that I end up creating in the thread. I am trying to learn threads in C and seems like I have grasped it, but

Sorting 2 arrays using 2 threads takes more time than sorting the 2 arrays one by one

天大地大妈咪最大 提交于 2019-12-11 07:05:51
问题 I have 2 unsorted arrays and 2 copies of these arrays. I am using two different threads to sort two arrays, then I am sorting other two unsorted array one by one. What I thought was that the thread process would be faster but it's not, so how does threads take more time? #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <pthread.h> struct thread_data { int count; unsigned int *arr; }; struct thread_data thread_data_array[2]; void insertionSort(unsigned int

Order of running threads in pthreads

自闭症网瘾萝莉.ら 提交于 2019-12-04 06:17:34
问题 In the following program, what are the possibilities for the ordering of threads? Assuming "function" will print thread id which is unique (since here we have only one process). I always get the order th1,th2! #include <stdlib.h> #include <stdio.h> #include <pthread.h> int main() { pthread_t th1; pthread_t th2; pthread_create(&th1, NULL, function, NULL); pthread_create(&th2, NULL, function, NULL); pthread_join(th1, NULL); pthread_join(th2, NULL); } return 0; } 回答1: The only ordering