0

Hello I am new to pthread and, trying to test this program.

http://www.ibm.com/developerworks/library/l-posix3/

This program is working very well, but one thing. what is this sleep() in main thread? if I change it to sleep(2) to shorter or putting more task on this *threadfunc.

The thread finish before completing the all tasks. Is there any better way to wait until the task finish?

2 Answers2

0

The sleep() suspend the main thread, so the worker threads do the work for a time. After the main thread wakes up, it deactivates the work queue, so wq.control.active in threadfunc() is false, the worker threads terminate no matter whether the threads finish their tasks.

jfly
  • 7,715
  • 3
  • 35
  • 65
0

Is there any better way to wait until the task finish?

Just join it using pthread_join(), the call would block until the thread to be joined ended.

alk
  • 69,737
  • 10
  • 105
  • 255