from multiprocessing import cpu_count


def set_threads(t):
    """
    Set maximum number of threads to use. When ``t=-1`` defaults to the value 
    returned by multiprocessing.cpu_count() 

    .. rubric:: Parameters

    t :  integer
        Number of threads desired

    .. rubric:: Returns

    t : integer
        Number of threads to use
    """
    maxt = cpu_count()
    if t<=0:
        t = maxt
    else:
        t = min(t,maxt)
    return t