pthread-join

A detached pthread causes memory leaks

倾然丶 夕夏残阳落幕 提交于 2019-12-04 03:28:07
问题 There is a known memory leak, when terminating a process with running undetached pthreads. However, detaching the thread doesn't seem to be a solution. Consider the following minimal example: #include <pthread.h> #include <stdio.h> static void* thread(void* _) { for(;;); return NULL; } int main(void) { pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_t tid; pthread_create(&tid, &attr, thread, NULL); pthread_attr_destroy(&attr)

Order of running threads in pthreads

流过昼夜 提交于 2019-12-02 08:54:09
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; } The only ordering guarantees here are that pthread_join(th1, NULL); will not return until thread 1 has exited and pthread_join(th2,

Java Multithreading concept and join() method

↘锁芯ラ 提交于 2019-11-26 23:37:53
I'm confused in join() method used in Threads in Java. In the following code: // Using join() to wait for threads to finish. class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for (int i = 5; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out

Java Multithreading concept and join() method

瘦欲@ 提交于 2019-11-26 08:44:47
问题 I\'m confused in join() method used in Threads in Java. In the following code: // Using join() to wait for threads to finish. class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println(\"New thread: \" + t); t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for (int i = 5; i > 0; i--) { System.out.println(name + \": \" + i);