Threads inside the thread?

I want to implement divide and win with pthread, but I don't know what will happen if I create more threads in the thread.

As far as I understand, if a machine has a dual-core processor, it can only process 2 threads at a time. If there are more than two threads, other threads have to wait for resources, so if I create more and more threads while I go deeper, in fact this may not increase the speed of the algorithm, since only 2 threads can be processed at the same time .

I am doing some research on the Internet, and it seems that threads at the top level can be inactive, only those at the deepest level remain active. How to achieve this? Also, if the upper thread remains inactive, does it affect the lower thread?

+3
source share
3 answers

There are two main types: disconnected and compatible.

A join thread is one that you can wait for (or access the result) with pthread_join.

Using more threads than there can help or hurt - depends on your program! It is often useful to minimize or eliminate contention for multithreaded access resources. Too many threads in a program can actually slow down the process. However, you will probably have free processor time if the number of cores matches the number of threads, and one of the threads expects an IO disk (if nothing happens in other processes).

, , , . ?

, , . , pthread_join. , .

, , , () , .

, ?

. , / . , , . , . , / /.

, , , , 2 .

, . - . .

+5

, , , , - :

void *
routine (void * argument)
{
  /* divide */
  left_arg = f (argument);
  right_arg = f (argument);

  /* conquor */
  pthread_create (left_child, NULL, routine, left_arg);
  pthread_create (right_child, NULL, routine, right_arg);

  /* wait for 'children' */
  pthread_join (left_child, &left_return_val);
  pthread_join (right_child, &right_return_val);

  /* merge results & return */
}

, " " :

void *
routine (void * argument)
{
  /* divide */
  left_arg = f (argument);
  right_arg = f (argument);

  /* conquor */
  pthread_create (left_child, NULL, routine, left_arg);
  /* do the right_child work yourself */
  right_return_val = routine (right_arg);

  /* wait for 'left child' */
  pthread_join (left_child, &left_return_val);

  /* merge results & return */
}

, N , . , - .. , P , , kP, , , " kP . , , kP , :

THREAD_POOL pool = new_thread_pool (k * P); /* I made this function up */

void *
routine (void * argument)
{
  /* divide */
  left_arg = f (argument);
  right_arg = f (argument);

  /* conquor */
  left_thread = get_worker (pool); /* Blocks until a thread is free  */
  /* get left_thread to do processing for you */

  right_thread = get_worker (pool); /* Blocks until a thread is free  */
  /* get right_thread to do processing for you */

  /* wait for 'children' */
  pthread_join (left_child, &left_return_val);
  pthread_join (right_child, &right_return_val);

  /* return the workers */
  put_worker (pool, left_thread);
  put_worker (pool, right_thread);

  /* merge results & return */
}
+2

, . , .

[] , ( ).

, 5 2 , 40% ( ). , , . , , .

, . :

  • : 1 -, 2; thread 2 -, 1
  • :
  • : 1 2, 2 , 1 2

I found this page ( http://ashishkhandelwal.arkutil.com/index.php/csharp-c/issues-with-multithreaded-programming-part-1/ ), which may be a good start for multi-threaded programming.

0
source

All Articles