erebus

diff liberebus/src/threadpool.cc @ 31:53a98c148bf8

- introduced SurfaceGeometry to carry all the geometric information input to BRDF sampling and evaluation functions. - made Reflectance keep an (optional) pointer to its material - simplified PhongRefl::sample_dir, with the help of SurfaceGeometry - worked around microsoft's broken std::thread implementation's deadlock on join
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 07 Jun 2014 09:14:17 +0300
parents c8a6fb04fefa
children b1fc96c71bcc
line diff
     1.1 --- a/liberebus/src/threadpool.cc	Sat Jun 07 06:10:21 2014 +0300
     1.2 +++ b/liberebus/src/threadpool.cc	Sat Jun 07 09:14:17 2014 +0300
     1.3 @@ -19,23 +19,52 @@
     1.4  	thread = new std::thread[num_threads];
     1.5  	for(int i=0; i<num_threads; i++) {
     1.6  		thread[i] = std::thread(&ThreadPool::thread_func, this);
     1.7 +
     1.8 +#ifdef _MSC_VER
     1.9 +		/* detach the thread to avoid having to join them in the destructor, which
    1.10 +		 * causes a deadlock in msvc implementation when called after main returns
    1.11 +		 */
    1.12 +		thread[i].detach();
    1.13 +#endif
    1.14  	}
    1.15  	this->num_threads = num_threads;
    1.16  }
    1.17  
    1.18  ThreadPool::~ThreadPool()
    1.19  {
    1.20 +#ifdef _MSC_VER
    1.21 +	workq_mutex.lock();
    1.22 +	workq.clear();
    1.23 +	qsize = 0;
    1.24 +	workq_mutex.unlock();
    1.25 +#endif
    1.26 +
    1.27  	quit = true;
    1.28  	workq_condvar.notify_all();
    1.29  
    1.30  	printf("ThreadPool: waiting for %d worker threads to stop ", num_threads);
    1.31  	fflush(stdout);
    1.32 +#ifndef _MSC_VER
    1.33  	for(int i=0; i<num_threads; i++) {
    1.34  		thread[i].join();
    1.35  		putchar('.');
    1.36  		fflush(stdout);
    1.37  	}
    1.38 +
    1.39 +#else
    1.40 +	// spin until all threads are done...
    1.41 +	std::unique_lock<std::mutex> lock(workq_mutex);
    1.42 +	while(nactive > 0) {
    1.43 +		lock.unlock();
    1.44 +		std::this_thread::sleep_for(std::chrono::milliseconds(128));
    1.45 +		putchar('.');
    1.46 +		fflush(stdout);
    1.47 +		lock.lock();
    1.48 +	}
    1.49 +#endif	// _MSC_VER
    1.50 +
    1.51  	putchar('\n');
    1.52 +	delete [] thread;
    1.53  }
    1.54  
    1.55  void ThreadPool::add_work(std::function<void ()> func)