// File: waket[P]ThreadPool/main.cpp
// Function: main
int main(int argc, char* argv[])
{
   int result = 0;
   cout << "The purpose of this code is to show the usage of ThreadPoolTask library." << endl << endl;
   srand ( time(NULL) );

   // Synchronous start and stop
   {
      ThreadPoolTask tpt;
      cout << "Adding Taks... " << endl;
      for (unsigned int i = 0; i < random() % 15; i++)
          tpt.doTask(hello);
      cout << "Adding task... DONE" << endl;

      cout << "Starting ThreadPoolTask... " << endl;
      if (tpt.start())
      {
         cout << "Starting ThreadPoolTask DONE" << endl;
      }
      else
      {
         cout << "Starting ThreadPoolTask ERROR" << endl;
      }

      cout << "Adding task... " << endl;
      for (unsigned int i = 0; i < random() % 15; i++)
          tpt.doTask(hello);
      cout << "Adding task... DONE" << endl;

      sleep(random() % 15);

      cout << "Stopping ThreadPoolTask... " << endl;
      if (tpt.stop())
      {
         cout << "Stopping ThreadPoolTask DONE" << endl;
      }
      else
      {
         cout << "Stopping ThreadPoolTask ERROR" << endl;
      }
   }
   // Synchronous start and asynchronous stop
   {
      ThreadPoolTask tpt;
      cout << "Adding task... " << endl;
      for (unsigned int i = 0; i < random() % 15; i++)
          tpt.doTask(hello);
      cout << "Adding task... DONE" << endl;

      cout << "Starting ThreadPoolTask... " << endl;
      if (tpt.start())
      {
         cout << "Starting ThreadPoolTask DONE" << endl;
      }
      else
      {
         cout << "Starting ThreadPoolTask ERROR" << endl;
      }

      cout << "Adding task... " << endl;
      for (unsigned int i = 0; i < random() % 5; i++)
          tpt.doTask(hello);
      cout << "Adding task... DONE" << endl;

      cout << "Set worker count to ... " << endl;
      if (tpt.setWorkerCount(3)) {
          cout << "Set worker count DONE" << endl;
      }
      else
      {
          cout << "Set worker count ERROR" << endl;
      }

      sleep(random() % 10);

      cout << "Requesting ThreadPoolTask stop... " << endl;
      if (tpt.requestStop())
      {
         cout << "Requesting ThreadPoolTask stop DONE" << endl;
      }
      else
      {
         cout << "Requesting ThreadPoolTask stop ERROR" << endl;
      }

//      cout << "Requesting ThreadPoolTask stop... " << endl;
//      if (tpt.requestStop())
//      {
//         cout << "Requesting ThreadPoolTask stop DONE" << endl;
//      }
//      else
//      {
//         cout << "Requesting ThreadPoolTask stop ERROR" << endl;
//      }
//
//      cout << "Adding task... " << endl;
//      for (unsigned int i = 0; i < random() % 15; i++)
//          tpt.doTask(hello);
//      cout << "Adding task... DONE" << endl;

      cout << "Blocking for stop... " << endl;
      if (tpt.wait())
      {
         cout << "Blocking for stop DONE" << endl;
      }
      else
      {
         cout << "Blocking for stop ERROR" << endl;
      }
   }
   // Done
   cout << "OK" << endl;
   return result;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x620, %rsp            # imm = 0x620
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x0, -0x14(%rbp)
movq 0xba61(%rip), %rdi      # 0xdfd8
movq %rdi, -0x500(%rbp)
leaq 0x6a92(%rip), %rsi      # 0x9017
callq 0x2180
movq %rax, %rdi
movq 0xba34(%rip), %rsi      # 0xdfc8
movq %rsi, -0x508(%rbp)
callq 0x21c0
movq -0x508(%rbp), %rsi
movq %rax, %rdi
callq 0x21c0
xorl %eax, %eax
movl %eax, %edi
callq 0x2160
movl %eax, %edi
callq 0x2170
leaq -0x230(%rbp), %rdi
callq 0x2f40
movq -0x500(%rbp), %rdi
leaq 0x6a87(%rip), %rsi      # 0x9060
callq 0x2180
movq %rax, -0x4f8(%rbp)
jmp 0x25e7
movq -0x4f8(%rbp), %rdi
movq 0xb9d3(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x25fc
movl $0x0, -0x240(%rbp)
movl -0x240(%rbp), %eax
movq %rax, -0x510(%rbp)
callq 0x20b0
movl $0xf, %ecx
cqto
idivq %rcx
movq -0x510(%rbp), %rax
cmpq %rdx, %rax
jge 0x26b5
leaq -0x199(%rip), %rsi      # 0x24a0
leaq -0x260(%rbp), %rdi
xorl %edx, %edx
callq 0x3150
jmp 0x2649
leaq -0x230(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x5570
jmp 0x265e
leaq -0x260(%rbp), %rdi
callq 0x3180
movl -0x240(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x240(%rbp)
jmp 0x2606
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x2a59
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x260(%rbp), %rdi
callq 0x3180
jmp 0x2a59
movq 0xb91c(%rip), %rdi      # 0xdfd8
leaq 0x69ad(%rip), %rsi      # 0x9070
callq 0x2180
movq %rax, -0x518(%rbp)
jmp 0x26d1
movq -0x518(%rbp), %rdi
movq 0xb8e9(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x26e6
movq 0xb8eb(%rip), %rdi      # 0xdfd8
leaq 0x6990(%rip), %rsi      # 0x9084
callq 0x2180
movq %rax, -0x520(%rbp)
jmp 0x2702
movq -0x520(%rbp), %rdi
movq 0xb8b8(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2717
leaq -0x230(%rbp), %rdi
callq 0x5210
movb %al, -0x521(%rbp)
jmp 0x272b
movb -0x521(%rbp), %al
testb $0x1, %al
jne 0x2737
jmp 0x276a
movq 0xb89a(%rip), %rdi      # 0xdfd8
leaq 0x695b(%rip), %rsi      # 0x90a0
callq 0x2180
movq %rax, -0x530(%rbp)
jmp 0x2753
movq -0x530(%rbp), %rdi
movq 0xb867(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2768
jmp 0x279d
movq 0xb867(%rip), %rdi      # 0xdfd8
leaq 0x6945(%rip), %rsi      # 0x90bd
callq 0x2180
movq %rax, -0x538(%rbp)
jmp 0x2786
movq -0x538(%rbp), %rdi
movq 0xb834(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x279b
jmp 0x279d
movq 0xb834(%rip), %rdi      # 0xdfd8
leaq 0x6930(%rip), %rsi      # 0x90db
callq 0x2180
movq %rax, -0x540(%rbp)
jmp 0x27b9
movq -0x540(%rbp), %rdi
movq 0xb801(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x27ce
movl $0x0, -0x264(%rbp)
movl -0x264(%rbp), %eax
movq %rax, -0x548(%rbp)
callq 0x20b0
movl $0xf, %ecx
cqto
idivq %rcx
movq -0x548(%rbp), %rax
cmpq %rdx, %rax
jge 0x286c
leaq -0x367(%rip), %rsi      # 0x24a0
leaq -0x288(%rbp), %rdi
xorl %edx, %edx
callq 0x3150
jmp 0x2817
leaq -0x230(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x5570
jmp 0x282c
leaq -0x288(%rbp), %rdi
callq 0x3180
movl -0x264(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x264(%rbp)
jmp 0x27d8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x3180
jmp 0x2a59
movq 0xb765(%rip), %rdi      # 0xdfd8
leaq 0x67f6(%rip), %rsi      # 0x9070
callq 0x2180
movq %rax, -0x550(%rbp)
jmp 0x2888
movq -0x550(%rbp), %rdi
movq 0xb732(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x289d
callq 0x20b0
movq %rax, %rcx
movabsq $-0x7777777777777777, %rdx # imm = 0x8888888888888889
movq %rcx, %rax
imulq %rdx
addq %rcx, %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x3, %rdx
addl %eax, %edx
movl %edx, %eax
leal (%rax,%rax,4), %edx
movl %edx, %eax
leal (%rax,%rax,2), %eax
movl %ecx, %edi
subl %eax, %edi
callq 0x2100
jmp 0x28da
movq 0xb6f7(%rip), %rdi      # 0xdfd8
leaq 0x6803(%rip), %rsi      # 0x90eb
callq 0x2180
movq %rax, -0x558(%rbp)
jmp 0x28f6
movq -0x558(%rbp), %rdi
movq 0xb6c4(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x290b
leaq -0x230(%rbp), %rdi
callq 0x5330
movb %al, -0x559(%rbp)
jmp 0x291f
movb -0x559(%rbp), %al
testb $0x1, %al
jne 0x292b
jmp 0x295e
movq 0xb6a6(%rip), %rdi      # 0xdfd8
leaq 0x67ce(%rip), %rsi      # 0x9107
callq 0x2180
movq %rax, -0x568(%rbp)
jmp 0x2947
movq -0x568(%rbp), %rdi
movq 0xb673(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x295c
jmp 0x2991
movq 0xb673(%rip), %rdi      # 0xdfd8
leaq 0x67b8(%rip), %rsi      # 0x9124
callq 0x2180
movq %rax, -0x570(%rbp)
jmp 0x297a
movq -0x570(%rbp), %rdi
movq 0xb640(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x298f
jmp 0x2991
leaq -0x230(%rbp), %rdi
callq 0x4a40
leaq -0x4a0(%rbp), %rdi
callq 0x2f40
movq 0xb628(%rip), %rdi      # 0xdfd8
leaq 0x6724(%rip), %rsi      # 0x90db
callq 0x2180
movq %rax, -0x578(%rbp)
jmp 0x29c5
movq -0x578(%rbp), %rdi
movq 0xb5f5(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x29da
movl $0x0, -0x4a4(%rbp)
movl -0x4a4(%rbp), %eax
movq %rax, -0x580(%rbp)
callq 0x20b0
movl $0xf, %ecx
cqto
idivq %rcx
movq -0x580(%rbp), %rax
cmpq %rdx, %rax
jge 0x2aa4
leaq -0x577(%rip), %rsi      # 0x24a0
leaq -0x4c8(%rbp), %rdi
xorl %edx, %edx
callq 0x3150
jmp 0x2a27
leaq -0x4a0(%rbp), %rdi
leaq -0x4c8(%rbp), %rsi
callq 0x5570
jmp 0x2a3c
leaq -0x4c8(%rbp), %rdi
callq 0x3180
movl -0x4a4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4a4(%rbp)
jmp 0x29e4
leaq -0x230(%rbp), %rdi
callq 0x4a40
jmp 0x2f33
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x2f27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x4c8(%rbp), %rdi
callq 0x3180
jmp 0x2f27
movq 0xb52d(%rip), %rdi      # 0xdfd8
leaq 0x65be(%rip), %rsi      # 0x9070
callq 0x2180
movq %rax, -0x588(%rbp)
jmp 0x2ac0
movq -0x588(%rbp), %rdi
movq 0xb4fa(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2ad5
movq 0xb4fc(%rip), %rdi      # 0xdfd8
leaq 0x65a1(%rip), %rsi      # 0x9084
callq 0x2180
movq %rax, -0x590(%rbp)
jmp 0x2af1
movq -0x590(%rbp), %rdi
movq 0xb4c9(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2b06
leaq -0x4a0(%rbp), %rdi
callq 0x5210
movb %al, -0x591(%rbp)
jmp 0x2b1a
movb -0x591(%rbp), %al
testb $0x1, %al
jne 0x2b26
jmp 0x2b59
movq 0xb4ab(%rip), %rdi      # 0xdfd8
leaq 0x656c(%rip), %rsi      # 0x90a0
callq 0x2180
movq %rax, -0x5a0(%rbp)
jmp 0x2b42
movq -0x5a0(%rbp), %rdi
movq 0xb478(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2b57
jmp 0x2b8c
movq 0xb478(%rip), %rdi      # 0xdfd8
leaq 0x6556(%rip), %rsi      # 0x90bd
callq 0x2180
movq %rax, -0x5a8(%rbp)
jmp 0x2b75
movq -0x5a8(%rbp), %rdi
movq 0xb445(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2b8a
jmp 0x2b8c
movq 0xb445(%rip), %rdi      # 0xdfd8
leaq 0x6541(%rip), %rsi      # 0x90db
callq 0x2180
movq %rax, -0x5b0(%rbp)
jmp 0x2ba8
movq -0x5b0(%rbp), %rdi
movq 0xb412(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2bbd
movl $0x0, -0x4cc(%rbp)
movl -0x4cc(%rbp), %eax
movq %rax, -0x5b8(%rbp)
callq 0x20b0
movl $0x5, %ecx
cqto
idivq %rcx
movq -0x5b8(%rbp), %rax
cmpq %rdx, %rax
jge 0x2c5b
leaq -0x756(%rip), %rsi      # 0x24a0
leaq -0x4f0(%rbp), %rdi
xorl %edx, %edx
callq 0x3150
jmp 0x2c06
leaq -0x4a0(%rbp), %rdi
leaq -0x4f0(%rbp), %rsi
callq 0x5570
jmp 0x2c1b
leaq -0x4f0(%rbp), %rdi
callq 0x3180
movl -0x4cc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4cc(%rbp)
jmp 0x2bc7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x4f0(%rbp), %rdi
callq 0x3180
jmp 0x2f27
movq 0xb376(%rip), %rdi      # 0xdfd8
leaq 0x6407(%rip), %rsi      # 0x9070
callq 0x2180
movq %rax, -0x5c0(%rbp)
jmp 0x2c77
movq -0x5c0(%rbp), %rdi
movq 0xb343(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2c8c
movq 0xb345(%rip), %rdi      # 0xdfd8
leaq 0x64a8(%rip), %rsi      # 0x9142
callq 0x2180
movq %rax, -0x5c8(%rbp)
jmp 0x2ca8
movq -0x5c8(%rbp), %rdi
movq 0xb312(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2cbd
leaq -0x4a0(%rbp), %rdi
movl $0x3, %esi
callq 0x5870
movb %al, -0x5c9(%rbp)
jmp 0x2cd6
movb -0x5c9(%rbp), %al
testb $0x1, %al
jne 0x2ce2
jmp 0x2d15
movq 0xb2ef(%rip), %rdi      # 0xdfd8
leaq 0x646b(%rip), %rsi      # 0x915b
callq 0x2180
movq %rax, -0x5d8(%rbp)
jmp 0x2cfe
movq -0x5d8(%rbp), %rdi
movq 0xb2bc(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2d13
jmp 0x2d48
movq 0xb2bc(%rip), %rdi      # 0xdfd8
leaq 0x644e(%rip), %rsi      # 0x9171
callq 0x2180
movq %rax, -0x5e0(%rbp)
jmp 0x2d31
movq -0x5e0(%rbp), %rdi
movq 0xb289(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2d46
jmp 0x2d48
callq 0x20b0
movq %rax, %rcx
movabsq $0x6666666666666667, %rdx # imm = 0x6666666666666667
movq %rcx, %rax
imulq %rdx
movq %rdx, %rax
shrq $0x3f, %rax
shrq $0x2, %rdx
addl %eax, %edx
addl %edx, %edx
movl %edx, %eax
leal (%rax,%rax,4), %eax
movl %ecx, %edi
subl %eax, %edi
callq 0x2100
jmp 0x2d7f
movq 0xb252(%rip), %rdi      # 0xdfd8
leaq 0x63fb(%rip), %rsi      # 0x9188
callq 0x2180
movq %rax, -0x5e8(%rbp)
jmp 0x2d9b
movq -0x5e8(%rbp), %rdi
movq 0xb21f(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2db0
leaq -0x4a0(%rbp), %rdi
callq 0x5790
movb %al, -0x5e9(%rbp)
jmp 0x2dc4
movb -0x5e9(%rbp), %al
testb $0x1, %al
jne 0x2dd0
jmp 0x2e03
movq 0xb201(%rip), %rdi      # 0xdfd8
leaq 0x63cd(%rip), %rsi      # 0x91ab
callq 0x2180
movq %rax, -0x5f8(%rbp)
jmp 0x2dec
movq -0x5f8(%rbp), %rdi
movq 0xb1ce(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2e01
jmp 0x2e36
movq 0xb1ce(%rip), %rdi      # 0xdfd8
leaq 0x63be(%rip), %rsi      # 0x91cf
callq 0x2180
movq %rax, -0x600(%rbp)
jmp 0x2e1f
movq -0x600(%rbp), %rdi
movq 0xb19b(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2e34
jmp 0x2e36
movq 0xb19b(%rip), %rdi      # 0xdfd8
leaq 0x63b0(%rip), %rsi      # 0x91f4
callq 0x2180
movq %rax, -0x608(%rbp)
jmp 0x2e52
movq -0x608(%rbp), %rdi
movq 0xb168(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2e67
leaq -0x4a0(%rbp), %rdi
callq 0x56a0
movb %al, -0x609(%rbp)
jmp 0x2e7b
movb -0x609(%rbp), %al
testb $0x1, %al
jne 0x2e87
jmp 0x2eba
movq 0xb14a(%rip), %rdi      # 0xdfd8
leaq 0x6375(%rip), %rsi      # 0x920a
callq 0x2180
movq %rax, -0x618(%rbp)
jmp 0x2ea3
movq -0x618(%rbp), %rdi
movq 0xb117(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2eb8
jmp 0x2eed
movq 0xb117(%rip), %rdi      # 0xdfd8
leaq 0x6359(%rip), %rsi      # 0x9221
callq 0x2180
movq %rax, -0x620(%rbp)
jmp 0x2ed6
movq -0x620(%rbp), %rdi
movq 0xb0e4(%rip), %rsi      # 0xdfc8
callq 0x21c0
jmp 0x2eeb
jmp 0x2eed
leaq -0x4a0(%rbp), %rdi
callq 0x4a40
movq 0xb0d8(%rip), %rdi      # 0xdfd8
leaq 0x6332(%rip), %rsi      # 0x9239
callq 0x2180
movq %rax, %rdi
movq 0xb0b2(%rip), %rsi      # 0xdfc8
callq 0x21c0
movl -0x14(%rbp), %eax
addq $0x620, %rsp            # imm = 0x620
popq %rbp
retq
leaq -0x4a0(%rbp), %rdi
callq 0x4a40
movq -0x238(%rbp), %rdi
callq 0x22c0
nop