(C ++) How can I pause the main thread and then resume another thread in it?

I don't work on Window at first. I tried the methods described here: to no avail.

Basically, I am creating a web browser that should suspend the main thread right before it outputs the results. The main thread should resume when my last pthread dies. I know that when my last pthread dies, I just don't know how to pause or resume the main thread.

Any help is much appreciated!

EDIT:

So, maybe only one workflow will exist at the point where I want to pause / resume main. I do this in the constructor, and threads appear when I collect more links.

+4
source share
3 answers

In the main thread, call pthread_join() for each worker thread.

+6
source

It looks like the base fork-join model will work for you:

Thread join is a protocol that allows a programmer to collect all the relevant threads at a logical synchronization point. For example, in fork-join parallelism, threads are created to solve parallel tasks and then join the main thread after completing their respective tasks (thereby fulfilling the implicit barrier when merging a point). Note that the thread that is performing the connection has stopped executing their corresponding thread function.

In the following example in the same document:

 int main(int argc, char **argv) { pthread_t thr[NUM_THREADS]; int i, rc; /* create a thread_data_t argument array */ thread_data_t thr_data[NUM_THREADS]; /* create threads */ for (i = 0; i < NUM_THREADS; ++i) { thr_data[i].tid = i; if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) { fprintf(stderr, "error: pthread_create, rc: %d\n", rc); return EXIT_FAILURE; } } /* block until all threads complete */ for (i = 0; i < NUM_THREADS; ++i) { pthread_join(thr[i], NULL); } return EXIT_SUCCESS; } 
+2
source

The main thread is the thread from which you control other threads. If you need to wait until all other threads are executed, why don't you follow them from the main thread. Creating another thread to control the main thread seems to be a design error.

0
source

Source: https://habr.com/ru/post/1397881/


All Articles