Use AC_SEARCH_LIBS instead of AC_CHECK_LIB

This is more portable solution and it adds needed args to $LIBS.
Add -pthread to CPPFLAGS when found

This allows better compilation optimization and may even broke
compilation if not specified and threads are used.